Allow falsy | T spreads for falsy primitives#18451
Conversation
Special-case types produced by `bool && expr` with the type `false | T`. This spreads `Partial<T>` instead of `false | T`.
And update tests to reflect that
|
Actually, this needs to work for all falsy types. This means that all primitives are allowed in spread types now. Not sure I like this change, but I'll leave it open for discussion. |
| if (type.types.length === 2) { | ||
| // getFalsyFlagsOfTypes | ||
| // getTypeFacts | ||
| const i = Math.max(type.types.indexOf(falseType), |
There was a problem hiding this comment.
why not use removeDefinitelyFalsyTypes on the union, then check the identity of the output vs. input to know if you should get add undefined to the member when you are building the normal spread type.
There was a problem hiding this comment.
Good idea. That also covers undefined and null, which I forgot.
| members.set(prop.escapedName, prop); | ||
| } | ||
| else { | ||
| const result = createSymbol(prop.flags | SymbolFlags.Optional, prop.escapedName); |
There was a problem hiding this comment.
can we merge this with the main spreadtype creation logic?
Only create properties once, only if needed, and don't create an intermediate anonymous type. The code is also inlined with the rest of `getSpreadType`.
|
I added the actual code from #16326 and confirmed that I do, in fact, need the code that creates the equivalent of So I kept the code but made it more efficient and integrated with the existing |
falsy | T spreads for falsy primitives
Intended for a different PR
Turns out partialising falsy unions wasn't needed -- I was just returning the wrong thing when spreading primitives.
|
OK, the ad-hoc |
Fixes #16326
Allow falsy union types like
false | Tornull | Tin spread in order to allow conditionally spreading:It's worth noting that this works currently without strictNullChecks because the type of
shouldSpread && spreadeeis B. However, with strictNullChecks it becomesfalse | B.This PR special-cases changes the
isValidSpreadTypecheck to allow falsy unions, which are unions that have (1) have one or more falsy types (2) whose non-falsy parts return true forisValidSpreadType. This is actually stricter than before; these spreads are no longer allowed:Earlier in the PR's history, I had to to create a bespoke
Partial<T>which was pretty clunky, but this code is not actually needed to fix the example from #16326, which I believe is by far the most common pattern.