简体   繁体   English

防止接口具有名称为 X 的属性

[英]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.如您所见,我有[propName : string] : any允许我拥有任何其他属性,但我需要禁止使用 name key属性。

in other words I want to allow any other property but key换句话说,我想允许除key任何其他属性

You could add an additional property key?: undefined to Config :您可以添加一个额外的属性key?: undefinedConfig

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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