Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript `Property is missing in type`, but the type is `never`

Tags:

typescript

What I want to achieve is to reference the original interface, and remove some attributes. This is my original interface:

export interface IInvitations {
    parent: Parse.User;
    visitor: Visitors;
    dates: IInvitationDateAndPin[];
}

When a user initiates the invitation, he should not give parent as a parameter. which will be auto-assigned before saving into the database. So I made a new interface to omit it:

export interface ICInvitations extends IInvitations {
    parent: never;
}

But then I cannot successfully assign value to this interface.

let data: ICInvitations = {
    visitor: Visitors,
    datas: [...]
}

It shows Property 'parent' is missing in type. But the type is never, which I failed to assign anything into it.

like image 502
Val Avatar asked Aug 13 '19 02:08

Val


People also ask

Is missing in type but required in type React?

The React. js error "Property is missing in type but required in type" occurs when we don't pass all of the required props to a component. To solve the error, make sure to pass all of the props the component requires, e.g. <MyComponent name="Tom" age={30} /> or mark its props as optional.

Is missing the following properties from type TypeScript?

The error "Type is missing the following properties from type" occurs when the type we assign to a variable is missing some of the properties the actual type of the variable expects. To solve the error, make sure to specify all of the required properties on the object.

Does not exist on type TypeScript?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

What is TypeScript never?

TypeScript introduced a new type never , which indicates the values that will never occur. The never type is used when you are sure that something is never going to occur. For example, you write a function which will not return to its end point or always throws an exception.


1 Answers

As Jeff mentioned, while you can't remove the property from the interface, you can reuse most of it by doing something like this (Omit is shipped with TypeScript 3.5):

export interface ICInvitations extends Omit<IInvitations, 'parent'> {
  parent?: never;
}

The parent?: never part is optional, but will let you get an error if anyone tries to construct something implementing this interface with a value set for parent.

like image 71
Oleg Vaskevich Avatar answered Oct 13 '22 15:10

Oleg Vaskevich