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

Boolean properties are treated differently in conditional types (functions/methods) #48696

Closed
bluebarcode opened this issue Apr 14, 2022 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@bluebarcode
Copy link

Bug Report

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

⏯ Playground Link

Playground link with relevant code

💻 Code

type FailsType = {
  foo: boolean;
};
type WorksType = { 
  foo: string
};
class TestClass<Param> {
  testFunc: <T extends Partial<Param>>(
    param: T
  ) => {testProp?:Param extends Partial<T> ? 'yes' : 'no'} = (param)=>({});
}
type inlineTest = FailsType extends Partial<FailsType> ? 'yes' : 'no';
const inlineTestValue:inlineTest = 'yes';

// Here i use the Works Type
const testClassWorks = new TestClass<WorksType>();
const resultWorks = testClassWorks.testFunc({ foo: 'works' });
resultWorks.testProp='yes';

// Here i use the Fails Type
const testClassFails = new TestClass<FailsType>();
const resultFails = testClassFails.testFunc({ foo: false });
resultFails.testProp='yes';

🙁 Actual behavior

The type of resultFails.testProp is not 'yes'

🙂 Expected behavior

The type of resultFails.testProp should be 'yes'

@bluebarcode
Copy link
Author

const testClassFails = new TestClass<FailType>();
const failParam = { foo: false }; // Step between!
const resultFails = testClassFails.testFunc(failParam);
resultFails.testProp='yes';

Interesting enough this works! the "typeof" used in the function seems to be slightly different...

@whzx5byb
Copy link

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 RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Apr 19, 2022
@RyanCavanaugh
Copy link
Member

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

@bluebarcode
Copy link
Author

bluebarcode commented 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
Copy link
Collaborator

This issue has been marked 'Working as Intended' 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
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants