简体   繁体   中英

prevent interface from having property with name X

I have this interface :

interface Config {
    height : number;
    width : number;
    [propName : string] : any;
}

as you can see I have [propName : string] : any which allow me to have any other properties but I need to disallow property with the name key for example.

in other words I want to allow any other property but key

You could add an additional property key?: undefined to Config :

interface Config {
    height: number;
    width: number;
    key?: undefined; // add this line
    [propName: string]: any;
}

const res1: Config = { height: 3, width: 4, me: "too" } // works
const res2: Config = { height: 3, width: 4, me: "too", key: "dsaf" } // error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM