Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Open
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I havesearched for related issues and found none that match my proposal.
- I have searched thecurrent rule list and found no rules that match my proposal.
- I haveread the FAQ and my problem is not listed.
My proposal is suitable for this project
- My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
- My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Description
Fromhttps://github.com/danielnixon/eslint-plugin-total-functions:
Optional properties (those with a ? after their name) interact badly with TypeScript's structural type system in a way that can lead to unsoundness.
...
This rule bans assignment from one type to another, if:
- the destination type has an optional property, and
- the source type has no matching property (either optional or otherwise).
Note that in order to take the function in here, we'll need to include the fixes proposed indanielnixon/eslint-plugin-total-functions#83.
Fail Cases
typeFoo={readonlyfoo:string};typeBar=Foo&{readonlybar?:()=>unknown};constthing={foo:"foo",bar:"bar"};constfoo:Foo=thing;constbar:Bar=foo;if(bar.bar!==undefined){bar.bar();// explodes at runtime}
Pass Cases
typeFoo={readonlyfoo:string};typeBar=Foo&{readonlybar?:()=>unknown};constthing={foo:"foo",bar:"bar"};constfoo:Foo=thing;constbar=foo;//@ts-expect-errorif(bar.bar!==undefined){//@ts-expect-errorbar.bar();// explodes, but now we know!}
Additional Info
Forking conversation out fromdanielnixon/eslint-plugin-total-functions#665.