DEV Community

Cover image for Communicate interface inheritance omission with type definition
j1ngzoue
j1ngzoue

Posted on • Updated on

Communicate interface inheritance omission with type definition

You can easily tell members about inheritance omissions using TypeScript type definitions.You can check the sample introduced here.

Suppose you have the following type definition:

interface CustomError {
    error?: {
        message:string,
        code:string,
    }
}

interface Response {
    user: {
        name: string
    }
}

type API = <T extends unknown>(req?: Request) => T 
Enter fullscreen mode Exit fullscreen mode

Response needs to inherit from CustomError, but you can't understand it without reading the type definition carefully.
In such a case, I will tell you the technique to easily tell the user.

Add a message prompting inheritance to the type definition.

type API = <T extends unknown>(req?: Request) 
=> T extends CustomError 
? T : 'CustomError interface inheritance required'
Enter fullscreen mode Exit fullscreen mode

The user can notice because a message prompting inheritance is displayed in the response when using.
Image description

I don't think it's the intended use, Maybe you should know it as one technique.

Top comments (0)