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
{{ message }}
Repository navigation
Boolean properties are treated differently in conditional types (functions/methods) #48696
Comparing conditional types containing boolean properties behave dififferent to other property types. But this only occures when the comparison takes place in a function/method.
🔎 Search Terms
conditional types, boolean, method, function
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ
typeFailsType={foo: boolean;};typeWorksType={foo: string};classTestClass<Param>{testFunc: <TextendsPartial<Param>>(param: T)=>{testProp?:ParamextendsPartial<T> ? 'yes' : 'no'}=(param)=>({});}typeinlineTest=FailsTypeextendsPartial<FailsType> ? 'yes' : 'no';constinlineTestValue:inlineTest='yes';// Here i use the Works TypeconsttestClassWorks=newTestClass<WorksType>();constresultWorks=testClassWorks.testFunc({foo: 'works'});resultWorks.testProp='yes';// Here i use the Fails TypeconsttestClassFails=newTestClass<FailsType>();constresultFails=testClassFails.testFunc({foo: false});resultFails.testProp='yes';
boolean is an alias for true | false, and when a primitive literal is contextually typed by a union containing that literal's corresponding type, then it retains its actual literal type instead of widening to its parent primitive. So the comparison here is { foo: boolean } extends { foo?: false } ? ... which is correctly false
This is not intuitive tho. But i assume there is good reason to be as it is? Especially since when i assign the object literal to a variable before it does indeed work.
Activity
bluebarcode commentedon Apr 14, 2022
Interesting enough this works! the "typeof" used in the function seems to be slightly different...
whzx5byb commentedon Apr 14, 2022
It is similar to #48363, which was fixed by #48380. But it seems the PR doesn't fix the issue when a boolean literal is used as a property.
RyanCavanaugh commentedon Apr 19, 2022
booleanis an alias fortrue | false, and when a primitive literal is contextually typed by a union containing that literal's corresponding type, then it retains its actual literal type instead of widening to its parent primitive. So the comparison here is{ foo: boolean } extends { foo?: false } ? ...which is correctlyfalsebluebarcode commentedon Apr 19, 2022
Thank you for the explanation!
This is not intuitive tho. But i assume there is good reason to be as it is? Especially since when i assign the object literal to a variable before it does indeed work.
typescript-bot commentedon Apr 21, 2022
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.