Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Excess property checks for destructured argumentsΒ #359

@mindplay-dk

Description

@mindplay-dk

I've struck up a longer discussion about type-checking of destructured arguments in a Typescript issue here and I was wondering if I could get your opinion on this matter.

Given this example:

type User = {
  id: number;
  name: string;
  email: string;
}

function stringifyUser({ name, email }: User): string {
  return `${name} (${email})`
}

Would it be reasonable to infer the argument type as Pick<User, "name" | "email">?

I mean, the underlying JS does not require an object with an id property, so why should this be required by the type-checker?

Hegel tends to be about accuracy, and I had actually hoped to find it already working like this.

But as they pointed out in the thread, only 4 people have ever asked for this feature, so maybe it's just not something anybody notice, cares or thinks about. From my perspective, pretty simple: I want functions with as few dependencies as are actually required.

And sure, I could type out Pick types all day, but why wouldn't the type-checker just check the types that are actually relevant to the function, instead of demanding properties that the function can't actually access?

That's how it works in JS, where destructuring arguments is the closest thing we have to a type-hint.

What do you think? πŸ€”

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions