1

Here is the problem, I declare a type A which is an object of specific type value.

type A = {
    [key: string]: {
        value: any;
        validate: string;
        error: null | string;
        required?: boolean;
    };
};

I declare a variable without using this type

let b = {
    name:{value:"", error:null, validate:"required"},
    email:{value:"", error:null, validate:"required"}
} 

Then I simply get the all keys type of the object b

type B = (keyof typeof b) // gives type "name" | "email"

Then I get all the keys automatically. Like this

type D = {
    [key in B]: {
        value: any;
        validate: string;
        error: null | string;
        required?: boolean;
    };
};

If I Declare variable b with the type A then

let b:A = {
    name:{value:"", error:null, validate:"required"},
    email:{value:"", error:null, validate:"required"}
} 

type B = (keyof typeof b) // gives type string | number

I want to assign variable b with type A and get all the keys so that I can use them in the D type.

Thank you for your time!

Soumen Khara
  • 128
  • 3
  • 5
  • No. I want the keys of an already typed object or something like that. – Soumen Khara Aug 04 '22 at 11:02
  • Is `type B = (typeof b)[(keyof typeof b)]` what you are looking for ? – Matthieu Riegler Aug 04 '22 at 11:42
  • 1
    @SoumenKhara The question I linked has almost the exact same problem you described: You have a variable. You want to give it **both** the type `A` **and** still extract type information from it but you noticed that you can only have one or the other. Read the answer of the post for a solution to this problem. – Tobias S. Aug 04 '22 at 11:45

0 Answers0