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
[Feature Request] Type guard on conditional types #23493
letx: Test<string,boolean>={multiple: true,value: "hello"};typeGuard<string,boolean>(x);/* Passes type-checker. input.multiple === true does not imply input.values is present.*/
Why not use a union type with a discriminant property? (requires --strictNullChecks).
Wenlu Wang (@Kingwl) probably no plans yet as marked as Working as intended. For most cases you can use discriminant properties to solve the problem, which Jack Williams (@jack-williams) posted. My case is more complicated than his (or mine, as I posted the simplified case to demo my idea) and it is unlikely to be done with discriminant properties. I'll post a demo if I have time.
I think the thing is that it can't be safely fixed due to Jack Williams (@jack-williams) example. The problem is that the types for Test can look like any of the following:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
jack-williams commentedon Apr 18, 2018
I'm not sure narrowing would be sound:
Why not use a union type with a discriminant property? (requires
--strictNullChecks).Kingwl commentedon Apr 23, 2018
have this issue any plan?🤫
Cryrivers commentedon Apr 23, 2018
Wenlu Wang (@Kingwl) probably no plans yet as marked as
Working as intended. For most cases you can use discriminant properties to solve the problem, which Jack Williams (@jack-williams) posted. My case is more complicated than his (or mine, as I posted the simplified case to demo my idea) and it is unlikely to be done with discriminant properties. I'll post a demo if I have time.Thanks.
Kingwl commentedon Apr 25, 2018
Zhongliang Wang (@Cryrivers)
thanks for your suggestion
I mean i'd like to try this if have not any plan or owner to fix thiskpdonn commentedon Apr 25, 2018
Wenlu Wang (@Kingwl)
I think the thing is that it can't be safely fixed due to Jack Williams (@jack-williams) example. The problem is that the types for
Testcan look like any of the following:It's the third case that makes
a valid assignment and would make the proposed narrowing unsound.
Although as a side note you can force
Test<string, boolean>to be a compile error by doing:Still doesn't narrow in the original example however since type constraints like that aren't taken into account.
Playground link of examples
typescript-bot commentedon May 11, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.