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
Unexpected error: TS2365: Operator '!==' cannot be applied to types 'false' and 'true' #12794
src/user_config_prompts.ts(17,6):
error TS2365: Operator '!==' cannot be applied to types 'false' and 'true'.
The full source is here and the build output here.
It seems like TypeScript has decided that the call to get can only return false, and therefore this comparison is bad; however I don't believe that's true - this method access stored state, so I can put some value in it and then reload my extension and it would receive the stored value.
This may be the same as #12772, but the response is confusing to me:
the function expression is not considered part of the flow, since it is not guaranteed to execute
This logic seems flawed - you can't assume something can never have a certain value because it might not be called; surely you have to assume it might have that value because you do not know if it will be called?
Reacted by Endel Dreyer, Alexander “weej” Jones and Ivan Nikulin
Indeed, there seems to be a bug in 2.1.4. (maybe a side effect of the new type inference).
interfaceA{get<T>(key: string,defaultValue: T): T;}leta: A;varx=a.get("key",false);// Ok, x is booleanvary=a.get("key",false)==true;// Error TS2365 Operator '==' cannot be applied to types 'false' and 'true'
I'm slightly confused - do you mean that all generic methods in all codebase like this will need to have loads of overloads (one for each primitive type)?
Surely it's incorrect to assume that if a literal value is passed as a generic arg that the return type is that specific value rather than its type? I can't imagine this would ever be the expected thing?
Surely it's incorrect to assume that if a literal value is passed as a generic arg that the return type is that specific value rather than its type? I can't imagine this would ever be the expected thing?
I do not see why this is true. the error message in the OP is a valid one. a.get("key", false) is false and not any boolean and a.get("key", false) == true is guaranteed to fail all the time. so not sure why i see this is a bad thing.
I don't understand why this is the case. When someone defines a generic method like T get<T>(default: T) surely they are saying that return type is the same type as default and not it is the same value as default. What would be the point of the method if it always just returned the default value?!
a.get("key", false) == true is guaranteed to fail all the time
The implementation of this method is in VS Code; I can persist a value and read it back. The default I pass is for if there is no stored value. If the stored value is true, then this method returns true. I'm confused why you think this would fail (what use would a method be if it only ever returns the exact value you passed it?).
true is a type, and so is false. boolean is true | false, and true === false is a comparison between two distinct types. nothing about values here. this is the same for numeric literals as well. 1 === 2 is an error as well.
The implementation of this method is in VS Code; I can persist a value and read it back. The default I pass is for if there is no stored value. If the stored value is true, then this method returns true. I'm confused why you think this would fail (what use would a method be if it only ever returns the exact value you passed it?).
the method declaration says it takes a type T and returns the same type T. why would it take a true and return boolean? would it be OK if the method took number | string and returned number?
true is a type, and so is false. boolean is true | false, and true === false is a comparison between two distinct types. nothing about values here. this is the same for numeric literals as well. 1 === 2 is an error as well.
I can't currently test; but if I pass a 1 as defaultValue to the function above would the return type be "the 1 type" or a number? If the "the 1 type" then I think that's also crazy, if a number, then this seems inconsistent.
I'm sure there are many advantages to treating true and false as their own types, but this behaviour feels really wonky to me. It seems most natural to consider true and false to be values of type bool and having to cast them to bools all over the place seems unnecessary and unwanted :(
Mohamed Hegazy (@mhegazy)
This works fine in TS 2.0.
Providing overloads for all primitives, makes the generic version useless.
There is surely something wrong with the new type inference.
functiongetLengthAsStr(strs: string[]): string{if(strs.length!==111){return"111"}// `strs.length` here can not be any thing other than `111`// this check is guaranteed to be trueif(strs.length!==222){return"222"}return"blah"}
Reacted by Adel KARZAZI, Aluan Haddad and James Carnegie
I also had this error, and after spending some time I figured out that this error is erroneously appearing when there's unreachable code. And sometimes even for reachable code, without an explanation.
Example:
classTrueFalse{one: boolean=true;two: boolean=false;publictest(){if(this.one==false&&this.two==false){console.log("that's true.")}elseif(this.one==false){console.log("that's false.")}elseif(this.one==false){// unreacheable code, removing gets rid of the errorconsole.log("that's false.")}}}lett=newTrueFalse();t.test();
Error: index.ts|12 col 18 error| Operator '==' cannot be applied to types 'true' and 'false'.
Your second else if is actually a bug that the compiler is catching for you. It's dead code. The branch will never be taken because the previous else's if checks for the same condition
Aluan Haddad (@aluanhaddad) I know, but the error message provided by the compiler is misleading. I also want to stress that sometimes this is happening even when there's no unreachable code, but I couldn't reproduce/isolate the scenario yet.
Activity
akarzazi commentedon Dec 9, 2016
Indeed, there seems to be a bug in 2.1.4. (maybe a side effect of the new type inference).
RyanCavanaugh commentedon Dec 12, 2016
Anders Hejlsberg (@ahejlsberg) thoughts on how to properly type this?
mhegazy commentedon Dec 12, 2016
you can define
Aas:RyanCavanaugh commentedon Dec 12, 2016
Probably worth providing overloads on all the primitives in that case
DanTup commentedon Dec 12, 2016
I'm slightly confused - do you mean that all generic methods in all codebase like this will need to have loads of overloads (one for each primitive type)?
Surely it's incorrect to assume that if a literal value is passed as a generic arg that the return type is that specific value rather than its type? I can't imagine this would ever be the expected thing?
mhegazy commentedon Dec 12, 2016
I do not see why this is true. the error message in the OP is a valid one.
a.get("key", false)isfalseand not anybooleananda.get("key", false) == trueis guaranteed to fail all the time. so not sure why i see this is a bad thing.DanTup commentedon Dec 13, 2016
I don't understand why this is the case. When someone defines a generic method like
T get<T>(default: T)surely they are saying that return type is the same type asdefaultand not it is the same value asdefault. What would be the point of the method if it always just returned the default value?!The implementation of this method is in VS Code; I can persist a value and read it back. The default I pass is for if there is no stored value. If the stored value is
true, then this method returnstrue. I'm confused why you think this would fail (what use would a method be if it only ever returns the exact value you passed it?).mhegazy commentedon Dec 14, 2016
trueis a type, and so isfalse.booleanistrue | false, andtrue === falseis a comparison between two distinct types. nothing about values here. this is the same for numeric literals as well.1 === 2is an error as well.the method declaration says it takes a type
Tand returns the same typeT. why would it take atrueand returnboolean? would it be OK if the method tooknumber | stringand returnednumber?DanTup commentedon Dec 14, 2016
I can't currently test; but if I pass a
1asdefaultValueto the function above would the return type be "the 1 type" or a number? If the "the 1 type" then I think that's also crazy, if a number, then this seems inconsistent.I'm sure there are many advantages to treating
trueandfalseas their own types, but this behaviour feels really wonky to me. It seems most natural to considertrueandfalseto be values of typebooland having to cast them to bools all over the place seems unnecessary and unwanted :(akarzazi commentedon Dec 14, 2016
Mohamed Hegazy (@mhegazy)
This works fine in TS 2.0.
Providing overloads for all primitives, makes the generic version useless.
There is surely something wrong with the new type inference.
This is a widely used pattern.
From : https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Configuration/AppSettingsBase.cs
https://github.com/ninject/Ninject/blob/master/src/Ninject/NinjectSettings.cs
https://github.com/Microsoft/vscode/blob/5bf21a60b655546857f8162fa9b14a60d0e52280/src/vs/workbench/api/node/extHostExtensionService.ts
mhegazy commentedon Dec 14, 2016
This was an intentional change in TS 2.1, please see https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#literal-types-are-inferred-by-default-for-const-variables-and-readonly-properties. not treating literal types as types causes other errors as well. i would recommend adding overloads for the primitive values to ensure you are returning the base type instead of the literal type as suggested above.
kipz commentedon Jan 4, 2017
Perhaps I'm missing something here too, but shouldn't this compile as it used to pre 2.1?
results in:
There are a few things we can do to make it compile, such as asserting the type:
or similarly:
or weirdly, replacing a return statement with something else:
mhegazy commentedon Jan 4, 2017
kipz commentedon Jan 5, 2017
Oh yeah, I see that, and cool that the compiler now catches that! :)
In sanitising the internal code, I removed the mutation:
which also fails to compile.
aluanhaddad commentedon Jan 15, 2017
Deleted user (@ghost)
You could write it like this:
where
_advancereturns abooleanand looks likeendel commentedon Jan 16, 2017
I also had this error, and after spending some time I figured out that this error is erroneously appearing when there's unreachable code. And sometimes even for reachable code, without an explanation.
Example:
aluanhaddad commentedon Jan 16, 2017
Your second else if is actually a bug that the compiler is catching for you. It's dead code. The branch will never be taken because the previous else's if checks for the same condition
endel commentedon Jan 16, 2017
Aluan Haddad (@aluanhaddad) I know, but the error message provided by the compiler is misleading. I also want to stress that sometimes this is happening even when there's no unreachable code, but I couldn't reproduce/isolate the scenario yet.
kipz commentedon Jan 16, 2017
Endel Dreyer (@endel) indeed, such as this: #12794 (comment)