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

Function returning Partial<T> allows incorrect objects #49821

Closed
nicolaslaplume opened this issue Jul 7, 2022 · 5 comments
Closed

Function returning Partial<T> allows incorrect objects #49821

nicolaslaplume opened this issue Jul 7, 2022 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@nicolaslaplume
Copy link

Bug Report

πŸ”Ž Search Terms

Partial

πŸ•— Version & Regression Information

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

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Person = {
    name: string;
    age: number;
}
const person:Partial<Person> = {name: 'Dave', ags: 3};
const getPerson: ()=>Partial<Person> = ()=>({name: 'Dave', ags: 3});

πŸ™ Actual behavior

There is a single error, when assigning directly to person.

πŸ™‚ Expected behavior

There are two errors, with the same error about Object literal may only specify known properties, and 'ags' does not exist in type 'Partial<Person>' for the returned value of getPerson

@nicolaslaplume
Copy link
Author

and it looks that doing a direct cast also prevents the error from showing:

const person:Partial<Person> = {name: 'Dave', ags: 3} as Partial<Person>;

@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 7, 2022

This is not a bug, and the objects are not incorrect. Additional properties are allowed.

The first error is due to excess property checks, which is more of a linter feature. You get the same behaviour in your second assignment, when you type your fat-arrow function accordingly (add a type annotation that it returns Partial<Person>).

Currently your function ()=>({name: 'Dave', ags: 3}) has no type annotation for the return type, so the return type is inferred. Then you assign this to a variable typed ()=>Partial<Person>, and the compiler checks if the type is assignable, which it is.

and it looks that doing a direct cast also prevents the error from showing:

A type assertion (often called a cast) is a way to tell the compiler "please ignore this obvious issue, I know what I'm doing".

@fatcerberus
Copy link

See also #12936 (proposal for Exact Types).

@jcalz
Copy link
Contributor

jcalz commented Jul 7, 2022

Also duplicate of #37017 and quite a few others, and ultimately #241

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 7, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

6 participants