Typescript Flashcards
Why can’t TypeScript Types affect the runtime performance of your code?
Because code generation is independent of the type system. The code that’s being run is native JavaScript.
Why you should avoid any
type?
Because it effectively silences the type checker and TypeScript language services.
What is a never
type?
It’s a TypeScript type with empty domain: no values are assignable to a variable with a never type.
What are union types?
Unions of sets of values, eg:
type AB = ‘A’ | ‘B’;
What is &
operator in type context?
The & operator computes the intersection of two types
Equation that illustrates the difference between Union and Intersection of interfaces?
keyof (A&B) = (keyof A) | (keyof B)
keyof (A|B) = (keyof A) & (keyof B)
What is the type’s domain
?
It’s a set of values assignable to it
How does typeof
operator differ between type and value space?
- In a type context, typeof takes a value and returns its TypeScript type.
- In a value context, typeof is JavaScript’s runtime typeof operator. It returns a string containing the runtime type of the symbol.
Why should you prefer type declarations to type assertions?
Type declaration verifies that the value conforms to the interface and throws an error otherwise. Type assertion does not.
What is “!” used as a suffix?
It is interpreted as an assertion that the value is non-null. It should be used only if you’re confident it’s in fact non-null.
When is it possible to use type assertions to convert between A and B types?
When A or B is a subset of the other.
Why isn’t it possible to set properties on primitive string values?
Because JS engine wraps it in String object on the fly to perform this operation and then throws this object away. This also applies to calling methods on primitives.
What’s excess property checking?
TypeScript feature that when you assign an object literal to a variable with a declared type, TypeScript makes sure it has the properties of that type and no others.