[英]non-standard TypeScript interface/class fields
I'm working on JSON-Schema-faker project where we use following TypeScript interface: 我正在JSON-Schema-faker项目上 ,我们在其中使用以下TypeScript接口:
interface IGeneratorSchema { faker?: any; chance?: any; }
interface IGeneratorSchema { faker?: any; chance?: any; }
for objects such as: { "faker": "name.findName" }
or: { "chance": { "bool": { "likelihood": 100 } } }
interface IGeneratorSchema { faker?: any; chance?: any; }
,例如: { "faker": "name.findName" }
或: { "chance": { "bool": { "likelihood": 100 } } }
Now we need to introduce support for x-faker
and x-chance
fields that would semantically mean the same as faker
and chance
, eg: 现在,我们需要引入对
x-faker
和x-chance
字段的支持,这些字段在语义上与faker
和chance
相同,例如:
{ "x-faker": "name.findName" }
or: { "x-chance": { "bool": { "likelihood": 100 } } }
{ "x-faker": "name.findName" }
或: { "x-chance": { "bool": { "likelihood": 100 } } }
I know I can create neither x-faker
nor x-chance
field in TypeScript interface. 我知道我无法在TypeScript界面中创建
x-faker
或x-chance
字段。 The question is - how can I overcome that? 问题是-我该如何克服? I want TypeScript to strictly allow only those four fields:
faker
, chance
, x-faker
, x-chance
. 我希望TypeScript仅严格允许这四个字段:
faker
, chance
, x-faker
和x-chance
。
I want TypeScript to strictly allow only those four fields: faker, chance, x-faker, x-chance
我希望TypeScript仅严格允许这四个字段:fakerr,机会,x-faker,x-chance
You can declare string properties: 您可以声明字符串属性:
interface IGeneratorSchema {
faker?: any;
chance?: any;
"x-faker"?: any;
"x-chance"?: any;
}
const foo:IGeneratorSchema = { "x-faker": {} } // Okay
const bar:IGeneratorSchema = { bad: {} } // Error: unknown property
Extra member are only prevented in fresh object literal scenarios : https://basarat.gitbooks.io/typescript/content/docs/types/freshness.html 仅在新的对象文字情况下才阻止额外成员: https : //basarat.gitbooks.io/typescript/content/docs/types/freshness.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.