I'm working on JSON-Schema-faker project where we use following TypeScript interface:
interface IGeneratorSchema { faker?: any; chance?: any; }
interface IGeneratorSchema { faker?: any; chance?: any; }
for objects such as: { "faker": "name.findName" }
or: { "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": "name.findName" }
or: { "x-chance": { "bool": { "likelihood": 100 } } }
I know I can create neither x-faker
nor x-chance
field in TypeScript interface. The question is - how can I overcome that? I want TypeScript to strictly allow only those four fields: faker
, chance
, x-faker
, x-chance
.
I want TypeScript to strictly allow only those four fields: faker, chance, 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
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.