public testOptions: "Undecided" | "Yes" | "No" = "Undecided";
Types that are separated with a pipe symbol |
are called union-types and can be read as an OR
operation. The =
sign in this case denotes an assignment. Meaning property testOptions
has the default value "Undecided"
Your code can be rewritten as:
// Foo is of type string, but not just any string, only the literal values
// "Undecided", "Yes", or "No". Any other string won't match the type.
type Foo = "Undecided" | "Yes" | "No";
// Will error because "bar" is not one of "Undecided", "Yes", or "No"
const a: Foo = "bar";
// Will work
const b: Foo = "Undecided";
To learn more about the advanced types in TypeScript, I'd highly recommend the docs on advanced types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With