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

Improve isValidSpreadType check #47010

Merged
merged 4 commits into from
Dec 4, 2021
Merged

Improve isValidSpreadType check #47010

merged 4 commits into from
Dec 4, 2021

Conversation

ahejlsberg
Copy link
Member

Fixes #46976.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Dec 3, 2021
return !!(type.flags & (TypeFlags.Any | TypeFlags.NonPrimitive | TypeFlags.Object | TypeFlags.InstantiableNonPrimitive) ||
getFalsyFlags(type) & TypeFlags.DefinitelyFalsy && isValidSpreadType(removeDefinitelyFalsyTypes(type)) ||
type.flags & TypeFlags.UnionOrIntersection && every((type as UnionOrIntersectionType).types, isValidSpreadType));
const t = removeDefinitelyFalsyTypes(mapType(type, getBaseConstraintOrType));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love that we have functions named getBaseConstraintOrType and getBaseConstraintOfType

@@ -20857,7 +20857,7 @@ namespace ts {
// flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns
// no flags for all other types (including non-falsy literal types).
function getFalsyFlags(type: Type): TypeFlags {
return type.flags & TypeFlags.Union ? getFalsyFlagsOfTypes((type as UnionType).types) :
return type.flags & TypeFlags.UnionOrIntersection ? getFalsyFlagsOfTypes((type as UnionType).types) :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little surprised that adding intersections to getFalsyFlags didn't change any existing baselines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, there are precious few intersections that would be affected, pretty much only those like T & undefined that will now appear to be falsy.

@ahejlsberg ahejlsberg merged commit b2af605 into main Dec 4, 2021
@ahejlsberg ahejlsberg deleted the fix46976 branch December 4, 2021 00:11
@RyanCavanaugh
Copy link
Member

Regression: #47028 (comment)

mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
* Fix getFalsyFlags for intersection types

* Fix and simplify isValidSpreadType

* Slight tweak

* Add tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(T | undefined) & T has different behavior when T is known versus when T is a type variable
4 participants