2 ms·
The benefit to having a type is mainly IDE experience and catching it at compile time. It's easiest to say `Genre.WESTERN` than using the string `western` and h
by ed25519FUUU 6y ago
The benefit to having a type is mainly IDE experience and catching it at compile time. It's easiest to say `Genre.WESTERN` than using the string `western` and hoping you didn't accidentally type `westrn` and get a runtime error.
- mssundaram 6y agoconst someBook: Book = { id: 1, name: "Title", genre: "wstern" // Compile error: [tsserver 2322] [E] Type '"westrn"' is not assignable to type 'Genre'. }; That's not how string literals work in Typescript. If you mis-type the string you'll get a compile error. It won't make it to runtime.
- wffurr 6y agoCompile time checks on the strings are exactly what you get with that TypeScript definition above.