3 ms·
Typescript doesn't have great syntax for object parameters. To define the type inline you have to repeat the property names. {a, b}: {a: string, b: string}
by seniorsassycat 5y ago
Typescript doesn't have great syntax for object parameters. To define the type inline you have to repeat the property names.
{a, b}: {a: string, b: string}
- gavinray 5y agoThis is one of my biggest pet-peeves. I write TS primarily. It seems like destructuring came out, and suddenly people destructure everything, even if it hurts readability. Stuff like this, I see constantly: const { id name, age, email, } = await getUser() I write it this way: const user = await getUser() // later user.name "user" here is typed, so you can hover over it/trigger autocomplete to see the props anyways. Same thing with functions, I see a lot of: function createUser({ id name, age }: { id: number name: string age: number }) { // code } Which, beyond just being ugly (though that's an opinion I guess) takes up an ABSURD amount of lines. I do this: interface CreateUserParams { id: number name: string age: number } function createUser(params: CreateUserParams) { // code }
- joshAg 5y agototally agree. if you need a one off type for a function argument, i definitely think it's much cleaner to make it a type or interface and declare it right before the function like you do in the final example rather than defining in-line.
- DangitBobby 5y agoThis is what I prefer as well, with the added benefit of easily being able to export the type for use somewhere else.
- leecommamichael 5y agoThis is a huge problem because I never want to type that, and it encourages people to flood the codebase with millions of little types.