TypeScript - TS keywords
There are a few keywords specific to TypeScript. They either extend basic types bound to the data types available inside JavaScript or provide options to work with existing values in order to create types based on them.
Each of them has specific use cases where they come in really handy.
Keywords - any and as
I know that I can use the any keyword to mark a variable that will accept basically anything.
I also understand that as can be used to force the type of a variable.
However, I am aware that using these types should be avoided, as it neglects all the benefits provided by TypeScript. Nevertheless, I understand that there are some cases where any and as can be necessary, as sometimes TypeScript will not be able to fully detect type narrowing.
Keywords - never and unknown
I understand the never and unknown keywords and their differences
I also know that for an unknown variable, it is better to mark it as unknown rather than any to enforce proper handling within the codebase
Keywords - typeof and keyof
I know that I can create types based on existing variables using typeof and keyof.
I understand that these keywords are based on static code analysis (not runtime type detection)
Keyword - as const
I know that I can mark an array or object as immutable using as const
I understand that it marks both the variable and its values as readonly
Keyword - satisfies
I know when to use the satisfies keyword. I know this feature has been available since TypeScript 4.9
I know that I can use the any keyword to mark a variable that will accept basically anything.
I also understand that as can be used to force the type of a variable.
However, I am aware that using these types should be avoided, as it neglects all the benefits provided by TypeScript. Nevertheless, I understand that there are some cases where any and as can be necessary, as sometimes TypeScript will not be able to fully detect type narrowing.
I understand the never and unknown keywords and their differences
I also know that for an unknown variable, it is better to mark it as unknown rather than any to enforce proper handling within the codebase
I know that I can create types based on existing variables using typeof and keyof.
I understand that these keywords are based on static code analysis (not runtime type detection)
I know that I can mark an array or object as immutable using as const
I understand that it marks both the variable and its values as readonly
I know when to use the satisfies keyword. I know this feature has been available since TypeScript 4.9