DoneDeal0
DoneDeal0

Reputation: 6257

How to change all types from interface to string?

I receive a form on a server. The form can only contain keys from User interface. But since it is encoded, all its values will be strings. I can parse them before inserting them in my database, but I need to inform typescript that all the data received have User keys with string type.

Basically, how to make this:

// base
interface User {
name: string;
hobbies: string[];
age: number;
}

// desired output
interface User {
name: string;
hobbies: string;
age: string;
}

Obviously, the model is quite long so it's not an option to copy/paste it and replace all its type with a string value.

Upvotes: 2

Views: 764

Answers (1)

zixiCat
zixiCat

Reputation: 1059

type NewUser = {
  [P in keyof User]: string;
};

Upvotes: 5

Related Questions