1

I have a interface

interface User {
    firstName: string;
    secondName: string;
    age: number;
}

I wanted to create a new type with different value type

type CustomT = { regex: RegExp }
interface UserWithRegexp {
    firstName: CustomT;
    secondName: CustomT;
    age: CustomT;
}

How can i refractor the UserWithRegexp type by using the User interface

I am expecting to have something like thistype UserWithRegexp = { [key of User] : CustomT }

1 Answer 1

1

How about using the Record<K, V> type?

type UserWithRegexp = Record<keyof User, CustomT>;

You should be able to use it like this.

const userWithRegex: UserWithRegexp = {
    firstName: { regex: /.*/ },
    secondName: { regex: /.*/ },
    age: { regex: /.*/ },
};
Sign up to request clarification or add additional context in comments.

1 Comment

You may want to note that the Record utility type is defined similarly to what the OP is asking for: type UserWithRegexp = { [key in keyof User] : CustomT }

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.