0

I'm working on JSON-Schema-faker project where we use following TypeScript interface:

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, e.g:

{ "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.

1

1 Answer 1

1

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

Caveat

Extra member are only prevented in fresh object literal scenarios : https://basarat.gitbooks.io/typescript/content/docs/types/freshness.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.