Skip to content

Pick doesn't validate correctlyΒ #55735

Closed
Closed

Description

πŸ”Ž 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.

Activity

  1. MartinJohns commented on Sep 13, 2023

    @MartinJohns
    Contributor

    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.

  2. guohaolay commented on Sep 13, 2023

    @guohaolay
    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.

  3. MartinJohns commented on Sep 13, 2023

    @MartinJohns
    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.

  4. guohaolay commented on Sep 13, 2023

    @guohaolay
    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.

  5. fatcerberus commented on Sep 13, 2023

    @fatcerberus

    Guo Hao (Andrew) Lay (@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

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions