This error occurs when there is a type mismatch between variables.
let firstName: string = "Felix";
firstName = true; // Type error: Type 'boolean' is not assignable to type 'string'Ensure that the assigned values match the specified type.
let firstName: string = "Felix";
firstName = "Joshua"; // No errorThis error occurs when you try to access a property that does not exist on an object.
interface Person {
name: string;
}
let person: Person = { name: "Alice" };
console.log(person.age); // Error: Property 'age' does not exist on type 'Person'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 definedThis error occurs when the argument type is not compatible with the expected type.
function greet(name: string) {
console.log("Hello, " + name);
}
greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'Ensure that the arguments match the types specified in the function signature.
function greet(name: string) {
console.log("Hello, " + name);
}
greet("Alice"); // No errorThis error occurs when you try to use a variable that has not been defined.
console.log(userName); // Error: Cannot find name 'userName'Ensure that the variable is defined before using it.
let userName: string = "Alice";
console.log(userName); // No errorThis error occurs when a type assertion is incorrect.
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 mistakeEnsure that the type assertion is correct.
let someValue: any = "this is a string";
let strLength: number = (someValue as string).length; // No errorThis error occurs when you try to access a property using a key that is not compatible with the index signature.
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'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 errorThis error occurs when there is a possibility that a value is undefined or null.
let user: { name: string } | null = null;
console.log(user.name); // Error: Object is possibly 'null'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
}