Common errors while working with TypeScript - Natalia
2.2. How to fix error "Type X is not assignable to type Y"
Understanding "Type X is not assignable to type Y"
- This error message appears when TypeScript's type checker detects a mismatch between the expected type and the actual type of a variable, parameter, or return value.
- The error is a signal that your code might not behave as expected at runtime, and TypeScript is helping you catch this issue early.
2.2.1. Mismatched Types
Attempting to assign a value of one type to a variable of another type.
Interpret and Fix: Jon Snow's age should be a number, not a string.
2.2.2. Object Shape Mismatch
The assigned object doesn't match the expected shape
Interpret and Fix: Jon Snow's age should be a number, not a string.
Depends on your design, it can either be fixed in one of the following ways:
2.2.3. Function Type Mismatch
Mismatch between function parameters or return types and their declared types
Interpret and Fix: Correct the function to return the number of dragons, not their names.
2.2.4. Array and Tuple Mismatch
Assigning an array or tuple to a type that expects a different structure or element types.
Interpret and Fix: Ensure all elements in the tuple are of the correct type, correcting Arya's age to her name.
2.2.5. Enum Mismatch
Assigning a value to an enum type that does not exist in the enum.
Interpret and Fix: The House enum does not include Baratheon
. Ensure you're assigning a value from the defined enum options.
2.2.6. Union Type Mismatch
Assigning a value that doesn't fit any type in a union.
Interpret and Fix: Ensure the value matches one of the union's types.
2.2.7. Incorrect Generic Types
Using a generic type that does not conform to the constraints or expected structure.
Interpret and Fix: Adjust the input to match the Character interface or extend the interface to include additional properties.
There are a few ways to fix depend on your use cases.
- Mark house as optional:
- Mark house as optional:
- Overloading the Function:
2.3. How to fix error "expression of type string cannot be used to index"
Challenge
2.3.1. Define a Function with a Specific Key Type
2.3.2. Type Assertion within a Function
2.3.3. Type Guard to Ensure Key Validity
2.3.4. Index Signature for Flexible Object Indexing
Another example:
2.4 How to handle variables that could be undefined
Problem:
2.4.1. Optional chaining
2.4.2. Nullish Coalescing Operator (??) - for ES11
2.4.3. Type Guards
2.4.4. Providing a Default Value
2.4.5. Type Assertion
only use it when you're absolutely sure 'title' is not undefined
2.5 How to fix error "'value' implicitly has an 'any' type"
2.5.1 Explicit Types or default value
2.5.2 Interfaces
2.5.3 Interfaces
2.6 How to fix error "Argument of type '..' is not assignable to parameter of type…"
2.6.1 Simply structuring the value
2.6.2 Type Assertion
Type assertions should be wielded with caution, for they tell TypeScript to trust the developer's knowledge over its own analysis.
2.7 How to fix error "Property Y does not exist on type X"
2.7.1 Simply define new interface
2.7.2 Extending Interfaces
2.7.3 Optional Properties
2.7.4 Index Signatures
2.8 How to fix error "Object literal may only specify known properties, and X does not exist in type Y"
2.8.1 Extending Interfaces
2.8.2 Optional Properties
Sometimes, not all details are known or necessary. We can mark some properties as optional:
2.8.3 Index Signatures
2.8.4 Type Assertion
Use this tool with caution, you're telling TypeScript to trust our intention.
Other example
2.9.1 Expected X Arguments, But Got Y
2.9.2 Expected X Arguments, But Got Y
2.9.3 Cannot Invoke an Expression Whose Type Lacks a Call Signature
2.9.4 A Function Whose Declared Type is Neither 'void' Nor 'any' Must Return a Value