Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick utility - error on excess values #56830

Closed
nitzcard opened this issue Dec 19, 2023 · 4 comments
Closed

Pick utility - error on excess values #56830

nitzcard opened this issue Dec 19, 2023 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@nitzcard
Copy link

nitzcard commented Dec 19, 2023

Using Pick on a function argument and then giving it a defined variable with excess properties do not give error like if I write it inline.

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about

⏯ Playground Link

https://www.typescriptlang.org/play?ssl=22&ssc=1&pln=1&pc=1#code/FASwdgLgpgTgZgQwMZQAQEFUG9itQgBwIBsoAuVMAVwFsAjWAbl1ToTHYQuvqeAF9gwCAE8CaAAogkAaygATVAF5UU2QB50AGlQByNhw66AfM2BwqYJBBAB7MKiQALKLIAUCGAHMAzhTVy8gCU2ILAAPThqAC0sXHxCYlJySkJwEj2PhCoAG7K2KycHBQAjDqEJOSoJaj8zM6uMm5YhYZc1eVEpKW1IZGoAOq2MDI++GPg0GDyCuku7jkhqP0+TrZUxIpeIDlo7KiwMMOoxCBy+HS2uxFRqXf3D-FC-UMjYwgTkFDTsxlgWfh-NJAvkcHgKt09HRiAhdFoWMN2F4qgAOeFhFgsIA

💻 Code

interface A {
  apple: number;
  banana: number;
}

type Picked = Pick<A, 'banana'>;

function check(args: Picked) {}

// --------------------------------------------------
const v = { banana: 1, apple: 1 };
check({ banana: 1, apple: 1 }) // Works as intended
check(v)  // should give an error like above
// --------------------------------------------------

// Works as intended
const a: Picked = {
  apple: 'bla',
  orange: 8,
}

🙁 Actual behavior

check(v) do not raise an error

🙂 Expected behavior

check(v) should raise an error

Additional information about the issue

No response

@Andarist
Copy link
Contributor

This is working as intended. Object types are not sealed in TypeScript and the excess property check is more like a lint rule than a strong type error. See the docs: https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks

@nmain
Copy link

nmain commented Dec 19, 2023

Pick<...> has no impact here and the results are exactly the same if you write:

type Picked = { banana: number };

@fatcerberus
Copy link

There’s a reason the error message specifies “Object literal can only specify known properties”; v is not an object literal. You’d need #12936 to enforce this across the board.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 19, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

6 participants