-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
type predicate inference: full union match results in loss of infered predicate "v is T" for a simple "boolean" #60741
Comments
This is working as designed; TS doesn't bother generating type predicates for things that don't narrow their inputs at all: https://github.com/microsoft/TypeScript/pull/57465/files#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8R37508 What exactly is the utility of a type guard function that doesn't narrow? Do you have some code whose behavior is actually improved when |
example effect-ts The issue is that the predicate does not have to be always static, it might also be dynamic so just picking "catchAll" is not a solution. I think the behavior is not "consistent" / "linear". Its like having a linear function but suddenly the same function gives you Y outside the line for certain X. In order for @Effect-TS to solve that they would have to assume that the inferred return type of the |
(I'm not a TS team member so feel free to ignore me) I don't think I can really commit to understanding Effect-TS to look at an example; do you have a self-contained example of such a "dynamic" predicate? Even if you provide this, I doubt they plan to modify the existing behavior to work for it, since it would cause lots of type predicates to appear in the wild for code that doesn't even attempt to narrow anything, triggering a whole bunch of extra control flow analysis where it doesn't need to happen. Consistency is nice, but it doesn't outweigh compiler performance. |
I'd be happy to look at this if there's a repro that is understandable in a reasonable amount of time (i.e. does not depend on effect-ts and is < 20-40 lines), but the repro as given isn't compelling for the reasons jcalz mentioned. |
A simpler Example without effect-ts: TS Playground |
It seems like someone writing that code has a bug, or at least has code suspicious enough to warrant additional verbiage to indicate that they know what they're doing. |
The code is 1:1 reduction of what @Effect-TS (the example) does. It takes a set A, a subset (B) of A and subtracts the subset B from set A. A subset can contain all elements of a set, means be equal. Then the difference is an empty set. In TS that is basically never. The logic by which you omit the predicate makes only sense in a subset of cases. That are those, where you don't inverse, subtract / reduce. In other cases it breaks. I can open an issue in @Effect-TS if you still think its a bug or mistake. PS. The nature of simple example is that its simple and will not reflect real life case. Effect TS uses that set difference to reduce the set of unhandled errors down the pipe line. This means, that the pipeline is potentially defined in one place, while the handling can vary and be passed as an argument to the function that creates the pipeline. Now changing the input predicate would mean, that also the pipeline implementation needs to be changed once all errors are handled. And this pipeline might even be in another library. How can I give you an example of that in 20-40 lines? |
60 lines? I don't know. Do what you need to do, but I have never seen a good type system change that couldn't be well-motivated by a single screenful of code. What does Effect-TS lose if it adds the non-predicate overload? |
π Search Terms
type predicate type guard inference union full complete match
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241211#code/C4TwDgpgBAYg9nKBeKBvUkBcUDk844A0UAZgtgHYCuAtgEYQBOAvgFAbQBCAho8mh2w4ejIlDq9sAZ2CMAlhQDmbDrAQj+6cBCH4RYsnBHY6CADYRuFNqwD0t8dwAmUALRQIAD0gBjYBCdsADduMyg5KTVEAB8oEQBuKEYIHwg5IICTc0sKVhIqCj85OAoofxkAQQAKELNsfChYkQBKKFsAKigsuAsrKHaHVFYoJIhgKkZS2oA6VSR5qAAifEXG2Jm5hcWRRdYbeyhFBCc8gqKSsogZThrQ+oRGuN5HvV5Wjq6oWvDIhqbngZoYajcaTL6hWbaZBbFZrcFmSGQaEoba8XbMIA
π» Code
π Actual behavior
The inferred return type of
testA
isboolean
π Expected behavior
The inferred return type of
testA
should beval is Foo | Bar
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: