3 ms·
This 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
by gavinray 5y ago
This 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.