Type ‘#’ Is Missing The Following Properties From Type ‘##’ – How To Fix It?

type ‘#’ is missing the following properties from type ‘##’

In this post, we will learn how to fix the error “Type ‘#’ is missing the following properties from type ‘##'”. Now, let’s follow some steps. I will show you how to fix it.

The cause of this error

The error “Type ‘#’ missing the following properties from type ‘##'” occurs when you use an object but inside it does not have the previously declared included properties or lacks properties of the object.

Let me show you when the error occurs:

Example 1:

class Student {
    name: string;
    age: number;
    address: string
}
 
const student: Student = {
};

Output:

Error: Type '{}' is missing the following properties from type 'Student': name, age, address ts(2739)

Example 2:

class Student {
    name: string;
    age: number;
    address: string
}
 
function getStudentInformation(student: Student) {
    return student;
}
 
getStudentInformation({});

Output:

Argument of type '{}' is not assignable to parameter of type 'Student'.
 Type '{}' is missing the following properties from type 'Student': name, age, address

In both examples, we set three types in the Student class but when we declare the student object there are no properties in it. The output we are expecting are ‘name’, ‘age’ and ‘address’ properties.

How to fix the error “Type ‘#’ is missing the following properties from type ‘##'”?

Method 1: Using Optional Mark ‘?’

Much of the time, we’ll locate ourselves dealing with objects that may have a property set. In this case, we can mark these properties as optional by adding a question mark ‘?’ at the end of the properties name.

For instance below the code:

class Student {
    name?: string; 
    age?: number; 
    address?: string
}
 
const student: Student = {
};
 
function getStudentInformation(student: Student) {
    return student;
}
 
getStudentInformation({});

‘name’, ‘age’, ‘address’ have been using question mark (?). No error message occurs because properties are not required. But they also aren’t needed.

Method 2: Setting default parameter for each properties

For example:

class Student {
    name: string;
    age: number;
    address: string
}
 
const student: Student = {
    name: '',
    age: 0,
    address: ''
};

Or, if you want to call a function that has an object, set the default parameter by the below example:

class Student {
    name: string;
    age: number;
    address: string;
}
 
function getStudentInformation(student: Student = {name: '', age: 0, address: ''}) {
    return student;
}
 
getStudentInformation();

Summary

The error “Type ‘#’ missing the following properties from type ‘##'” occurs when you use an object but inside it does not have the previously declared included properties or lacks properties of the object. After reading this post, I hope you will learn how to fix this error. I hope this tutorial is helpful to you. Happy coding.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *