You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
interfaceTodo{title: string;description: string;completed: boolean;}typeTodoPreview=Pick<Todo,"title"|"completed">;constdata:Todo={title: "Clean room",completed: false,description:"123",};// TypeScript correctly identifies the extra key `description`:consttodo: TodoPreview={title: "Clean room",completed: false,description:"123",};// But doesn't pick up if assigned this way:consttodo2: 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.
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.
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.
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
Reacted by Guo Hao (Andrew) Lay, Ryan Cavanaugh and Luke Oliff
Activity
MartinJohns commentedon 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.
guohaolay commentedon Sep 13, 2023
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 commentedon Sep 13, 2023
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 commentedon Sep 13, 2023
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 commentedon Sep 13, 2023
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