N
N
Ne7Le4Der2022-02-07 13:49:25
typescript
Ne7Le4Der, 2022-02-07 13:49:25

How to get a list of fields from the interface?

There is an interface

export interface Credentials {
    login: string | undefined;
    password: string | undefined;
}


I want to save RegExp in a separate object to check properties. Can I get a list of properties from Credentials so that I can create only the fields available in Credentials in the object? Something like this, only that the first argument for Record itself is pulled from Credentials

const credTester: Record<'login' | 'password', RegExp> = {
        login: /^[0-9a-zA-Z]*$/,
        password: /^[[email protected]#.]*$/
};

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry Belyaev, 2022-02-07
@Ne7Le4Der

const credTester: Record<keyof Credentials, RegExp> = {
        login: /^[0-9a-zA-Z]*$/,
        password: /^[[email protected]#.]*$/
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question