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 doesn't validate correctly #55735

Closed
guohaolay opened this issue Sep 13, 2023 · 5 comments
Closed

Pick doesn't validate correctly #55735

guohaolay opened this issue Sep 13, 2023 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@guohaolay
Copy link

guohaolay commented Sep 13, 2023

πŸ”Ž Search Terms

Pick, linter

πŸ•— Version & Regression Information

  • This is the behavior in version 4.0.7, and whatever the TypeScript playground uses

⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgCoHsAm7kG8BQyyYwYANhAFzIDOYUoA5gNyHKYQ0IMAOJ6IanQYgWbBOgC2PCpEzUARunQU4IVgF98+MAE8eKDNgAKUCADdgEAO7IAvMmPAEAawA8R9ABpkAIhLkEL7IAD5+EtKyEJi+AHys+BIgdOxwYHCUnvZ4bAEU1L4AwqogyFDKkr5e4lIyEHLU8GQ0ENVEHFy8-IK+AIwATADMVfgaCUkpYFjo1J6mFla2DphpcAlT2KwA9FvIAHoA-EA

πŸ’» Code

interface Todo {
  title: string;
  description: string;
  completed: boolean;
}

type TodoPreview = Pick<Todo, "title" | "completed">;

const data:Todo = {
  title: "Clean room",
  completed: false,
  description:"123",
};

// TypeScript correctly identifies the extra key `description`:
const todo: TodoPreview = {
  title: "Clean room",
  completed: false,
  description:"123",
};

// But doesn't pick up if assigned this way:
const todo2: TodoPreview = data;

πŸ™ Actual behavior

No error reported

πŸ™‚ Expected behavior

Error should be reported

Additional information about the issue

Pick does not validate correctly if using an object with more keys than the Picked key.

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 13, 2023

This is working as intended. Objects are allowed to have additional properties. Only in specific cases (like assignment of object literals) an "excess property check" is performed, but this is more of a linter feature than part of the type system.

And please fill out the issue template correctly. The "Code" section is empty and the search terms you used are excessive.

And TypeScript 4.0.7 is ancient. You might want to update.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Sep 13, 2023
@guohaolay
Copy link
Author

Thanks for the quick response (and I fixed my ticket, sorry about that).

While I agree that objects can have additional properties, this inconsistency is confusing and conflicting.

@MartinJohns
Copy link
Contributor

It's not inconsistent, and confusing only for beginners. TypeScript has a structural type system, and only object literals get excess property checks.

You might be interested in #12936, but don't expect it any time soon.

@guohaolay
Copy link
Author

I thought TypeScript's mission is to help all levels of developers, to say it's confusing only for beginners kind of defeats the purpose of having it. That proposal does looks great, thanks for sharing.

@fatcerberus
Copy link

@guohaolay It's known that the current error message for excess property check is confusing and suggests a type rule that doesn't actually exist. The message will change in 5.3, see #55152

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

4 participants