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
functionnumberToString<T>(value: T): Exclude<T,number>{returntypeofvalue==='number' ? `${value}` : value;// ^^^^^^ Error}// usagefunctionselectText(text: string|RegExp){// todo}functionanotherFn(arg: number|string|RegExp){// this is actually working, but somehow Line 2 is still raising the errorreturnselectText(numberToString(arg));}
🙁 Actual behavior
Type 'string | T' is not assignable to type 'Exclude<T, number>'.
Type 'string' is not assignable to type 'Exclude<T, number>'
🙂 Expected behavior
There should be no issue, since T is extending any by default, hence string should absolutely be included in Exclude<T, number>, where it should be any type but without number
The text was updated successfully, but these errors were encountered:
This would probably be a duplicate of #33912 if the return type were changed to T extends number ? `${T}` : T to correspond to what the function is actually doing.
Currently if you call numberToString(10), then T is inferred as 10, making the return type Exclude<10, number> which is never. But the function implementation returns a string, which (like all values) is not assignable to never. So the compiler error is a good one.
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?ts=5.1.0-dev.20230507#code/GYVwdgxgLglg9mABGEBbARgUwE4BU4DKU2MYA5gDy4B8AFAG4CGANiJgFyK4CUnAogA8IrACaYqAGmRos2aogDeAWABQidYmyYoIbEigBPAA6Y4wRE1aZEAXjuIA5Cgw4HiAPyIABgBIFltgBfL0ROAMwAblVA1VUAejjEEABnRjJMVVBIWAREZMxmTGhcTAEoWihSqE5k4lIyRAAfRAAlTDJBI25FVQ1EBMQoOBE4aNiVLOh4JEYwOCgACxwAMTBaRmwyTmdZJry68j22joEunrUNAcWYZMQbxEZoEBZmA0QAdzhsAGt6qXQQFA8nBUJgFnB3ogADKkawAJjut1qMGYzE0jBu9UGS0QOGwX16Gi0Oj0eQKRSgJTKtB2OHwRBI5HWm243CiKhiKnGkxySFpeEIBzIcKodHCnB4EtxZUwYBEt35Hm8flwwVCXHOfWJun0xlM5nCtnsThkriVvn8LCCITCVsiYy5E3AU1y-PpQoAzKKGHaJbwNVVZfLpC5sEraoyGlLlBd1NrSYYTGYLHajTZHPy3J4LeE1barOzOUA
💻 Code
🙁 Actual behavior
🙂 Expected behavior
There should be no issue, since T is extending
any
by default, hence string should absolutely be included inExclude<T, number>
, where it should beany
type but withoutnumber
The text was updated successfully, but these errors were encountered: