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
Conditional Types - Checking extends never only works sometimes #23182
typeIsNonNullableType<T>=IsNullableType<T>extendsnever ? true : never;assert<IsNonNullableType<string>>(true);// no compile error, works fine// this doesn't cause a compile error in playground, but it correctly throws a compile error in the latest versionassert<IsNonNullableType<string|undefined>>(true);// compile error, as expected
I'm just thinking this might be expected behaviour. So when a type is passed never (as T) and there's an expression like T extends ... ? ... : ..., then it will resolve that entire T extends ... ? ... : ... to never?
Anyway, I resolved my issue by making everything work with true or false, which is what I probably should have been doing from the start anyway... just couldn't figure out the right way to do it, but Extract helped.
Reacted by David Sherret, Ryan Cavanaugh, zhao5363, Anton Ovechkin, Yuriy Bogomolov, Spencer Yue, Paul Draper, Sam A. Horvath-Hunt, Keonwoo Kim, Alex Zinkevych and 81 more
Reacted by Yaojian, Bartłomiej Żuber, Tim Kinnane, Szymon Schläpfer, Sergey M., Gamote, Mikhail Kovalchuk, Joseph Garrone, Piotr, Victor Machado and 7 more
Reacted by Paul Draper, Bartłomiej Żuber, akachicon, Tim Kinnane, Joshua Harris, Chase, Oleg Valter, Haytham Halimeh, Gamote, Nick Dugger and 14 more
Reacted by Valery Zinchenko, mike, Igal Klebanov, Sergey and Ahmed Ali
Reacted by Adam Pietrasiak, Mikhail Kovalchuk, mike, Abhishek Rawat, 최호철 and Ahmed Ali
// Actual instantiation of 'T' shouldn't matter - conditional returns 'number' either wayfunctionassert<T>(expectTrue: Textendsnever ? number : number){}// OKassert<boolean>(0);assert<string>(0);assert<true>(0);// Error; argument of type '0' not assignable to 'never'assert<never>(0);
Reacted by David Sherret, Mathieu CAROFF, Alex Zinkevych and Kane Wang
As far as my understanding goes the conditional doesn't always return number, it can also return never because it may distribute over nothing.
// Ensure conditional is always `number`.functionassert<T>(expectTrue: [T]extends[never] ? number : number){}// OKassert<boolean>(0);assert<string>(0);assert<true>(0);assert<never>(0);
Reacted by Ryan Cavanaugh, Joe Calzaretta, Viktor, zhao5363, Petr Brabec, Jessica Franco, Oleksandr, Bartłomiej Żuber, David Richey, Vitaliy Stoliarov and 6 more
Reacted by Viktor, zhao5363, Petr Brabec, Jessica Franco, Bartłomiej Żuber, Adam Misiorny and Mew
After reading the code I agree with Jack Williams (@jack-williams) 's assessment. In this case never behaves as the empty union, so it distributes over the conditional and produces another empty union (which is just never again)
Reacted by Viktor, Paul Draper, Sam A. Horvath-Hunt, Axmor, Jessica Franco, Bartłomiej Żuber, Farzad Senart, David Richey, Yiou Chen, Valentin Palkovic and 8 more
typeNeverAlternative<T,P,N>=[T]extends[never] ? P : N;// and thentypeVariableLengthArray<A,B=never,C=never>=//NeverAlternative<C,NeverAlternative<B,[A],[A,B]>,NeverAlternative<B,[A,C],[A,B,C]>>;typeFoo=VariableLengthArray<1,2>// type [1, 2]typeBar=VariableLengthArray<1,2,3>// type [1, 2, 3]
After reading the code I agree with Jack Williams (@jack-williams) 's assessment. In this case never behaves as the empty union, so it distributes over the conditional and produces another empty union (which is just never again)
Where is the code to do these things? The TypeScript checker code is very complex. I can't find the specific code.
Activity
dsherret commentedon Apr 5, 2018
I'm just thinking this might be expected behaviour. So when a type is passed
never(asT) and there's an expression likeT extends ... ? ... : ..., then it will resolve that entireT extends ... ? ... : ...tonever?Anyway, I resolved my issue by making everything work with
trueorfalse, which is what I probably should have been doing from the start anyway... just couldn't figure out the right way to do it, butExtracthelped.jack-williams commentedon Apr 5, 2018
IsNeverTypeneeds to be implemented like:to stop it being distributive.
RyanCavanaugh commentedon Apr 5, 2018
Seems like a bug to me?
jack-williams commentedon Apr 5, 2018
As far as my understanding goes the conditional doesn't always return
number, it can also returnneverbecause it may distribute over nothing.RyanCavanaugh commentedon Apr 5, 2018
After reading the code I agree with Jack Williams (@jack-williams) 's assessment. In this case
neverbehaves as the empty union, so it distributes over the conditional and produces another empty union (which is justneveragain)dsherret commentedon Apr 5, 2018
Ryan Cavanaugh (@RyanCavanaugh) good to know! Thanks for looking into this and explaining the behaviour.
Jack Williams (@jack-williams) thanks a lot for that trick! Very helpful! 😄
jcalz commentedon Jun 6, 2018
Holy cow, this just bit me. The empty union!
pie6k commentedon Feb 7, 2020
I've created simple helper type
loynoir commentedon Nov 11, 2021
Jack Williams (@jack-williams) WOW! Awesome! Thanks a lot!
Is it safe to change return
neverto returnfalse?SyMind commentedon Sep 13, 2022
Where is the code to do these things? The TypeScript checker code is very complex. I can't find the specific code.
Fix more never identification (microsoft/TypeScript#23182) to make th…
No Never