Skip to content

Latest commit

 

History

History
146 lines (118 loc) · 3.46 KB

File metadata and controls

146 lines (118 loc) · 3.46 KB

Common TypeScript Errors and Solutions

1. Type Error

Description

This error occurs when there is a type mismatch between variables.

Code

let firstName: string = "Felix";
firstName = true; // Type error: Type 'boolean' is not assignable to type 'string'

Solution

Ensure that the assigned values match the specified type.

let firstName: string = "Felix";
firstName = "Joshua"; // No error

2. Property Does Not Exist

Description

This error occurs when you try to access a property that does not exist on an object.

Code

interface Person {
    name: string;
}

let person: Person = { name: "Alice" };
console.log(person.age); // Error: Property 'age' does not exist on type 'Person'

Solution

Ensure that the property exists in the interface or object.

interface Person {
    name: string;
    age?: number; // Optional property
}

let person: Person = { name: "Alice" };
console.log(person.age); // No error if 'age' is defined

3. Argument of Type is Not Assignable

Description

This error occurs when the argument type is not compatible with the expected type.

Code

function greet(name: string) {
    console.log("Hello, " + name);
}

greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

Solution

Ensure that the arguments match the types specified in the function signature.

function greet(name: string) {
    console.log("Hello, " + name);
}

greet("Alice"); // No error

4. Cannot Find Name

Description

This error occurs when you try to use a variable that has not been defined.

Code

console.log(userName); // Error: Cannot find name 'userName'

Solution

Ensure that the variable is defined before using it.

let userName: string = "Alice";
console.log(userName); // No error

5. Type Assertion Error

Description

This error occurs when a type assertion is incorrect.

Code

let someValue: any = "this is a string";
let strLength: number = (someValue as number).length; // Error: Conversion of type 'string' to type 'number' may be a mistake

Solution

Ensure that the type assertion is correct.

let someValue: any = "this is a string";
let strLength: number = (someValue as string).length; // No error

6. Index Signature Error

Description

This error occurs when you try to access a property using a key that is not compatible with the index signature.

Code

interface StringArray {
    [index: number]: string;
}

let myArray: StringArray = ["Alice", "Bob"];
let myStr: string = myArray["0"]; // Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'StringArray'

Solution

Ensure that the correct type of key is used.

interface StringArray {
    [index: number]: string;
}

let myArray: StringArray = ["Alice", "Bob"];
let myStr: string = myArray[0]; // No error

7. Possibly Undefined or Null

Description

This error occurs when there is a possibility that a value is undefined or null.

Code

let user: { name: string } | null = null;
console.log(user.name); // Error: Object is possibly 'null'

Solution

Ensure that the value is not null before accessing its properties.

let user: { name: string } | null = null;
if (user) {
    console.log(user.name); // No error
}

8