This content originally appeared on DEV Community and was authored by Nhan Nguyen
Give an example, we define a Color type:
type Color = "primary" | "secondary" | string;
Then we use it like this:
const color: Color = "primary";
But there's an issue:
We aren't getting color suggestions when we use the Color type.
We want primary and secondary to be on that list. How do we manage that?
We can intersect the string type in Color with an empty object like this:
type Color = "primary" | "secondary" | (string & {});
Now, we'll get suggestions for primary and secondary when we use the Color type.
I hope you found it helpful. Thanks for reading. 🙏
Let's get connected! You can find me on:
- Medium: https://medium.com/@nhannguyendevjs/
- Dev: https://dev.to/nhannguyendevjs/
- Hashnode: https://nhannguyen.hashnode.dev/
- Linkedin: https://www.linkedin.com/in/nhannguyendevjs/
- X (formerly Twitter): https://twitter.com/nhannguyendevjs/
- Buy Me a Coffee: https://www.buymeacoffee.com/nhannguyendevjs
This content originally appeared on DEV Community and was authored by Nhan Nguyen
Nhan Nguyen | Sciencx (2024-06-26T09:37:46+00:00) TypeScript What ‘string & {}’ mean meaning?. Retrieved from https://www.scien.cx/2024/06/26/typescript-what-string-mean-meaning/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.