0

Well, when I started with Angular I came in touch with Typescript the first time - and I did a huge mistake: I used always any as datatype. So now I have a few 10.000 lines of code and want to make it "type-save". Better late then never... and here comes the problem:

I have an object that has a property validity with a variable number of key-values, like

obj.validity = { 
    time: true,
    feePayed: true,
    approved: true
}

or

obj.validity = { 
    time: true,
    feePayed: true,
}

or

obj.validity = { 
    startpass: true,
    passport: true
}

the possible keys are defined in an enum:

export enum VerificationType {
    TIME = 'time',
    STARTPASS = 'startpass',
    PASSPORT = 'passport',
    FEEPAYED = 'feePayed',
    ALLOWPAYPENT = 'allowPayment',
    MANUALRELEASEPAYMENT = 'manualReleasePayment',
    MANUALAPPROVE = 'manualApprove',
    ALLPAYED = 'allPayed',
    APPROVED = 'approved',
}

In the object declaration I tried now something like that:

class obj {
    validity: ISkaterValidation
}

and

export type ISkaterValidation = {
    [key in VerificationType]: boolean;
};

This fails with error TS2339: Property 'ANY PROPERTY NAME' does not exist on type 'ISkaterValidation'.

I'm still a beginner with Typescript typings and hope that somebody can give me a solution, please.

1 Answer 1

2

Your type expects every key inside the object. You need to make it optional:

export type ISkaterValidation = {
    [key in VerificationType]?: boolean;
};

That tells TypeScript that the key doesn't have to exist on the type, but also no other key should be there.

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.