-
Notifications
You must be signed in to change notification settings - Fork 12.8k
String literal type unexpectedly widened to string #54767
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
Comments
Duplicate of #33912. Your issue is not a widening to |
It's not specific to return types. If instead of returning, we try to pass the value as function argument, the same behavior can be observed.
Thank you for the explanation @MartinJohns ! The error message could be more precise.
Could the compiler do "partial evaluation" of conditional types? E.g. if we constrain the function's type parameter with |
I can not reproduce this. Playground link
There's an issue about this, but I can't find it right now. I wouldn't expect this supported any time soon. It also wouldn't solve your issue, because of the recursiveness of your type - it's still a conditional type for the deeper levels. |
This is possible with a few workarounds. Critically, your conditional type can't be distributive. type StringKeyOf<T> = Exclude<keyof T, symbol>;
type JsonPointer<T> = [T] extends [object]
? { [K in StringKeyOf<T>]: `/${K}${JsonPointer<T[K]>}` | `/${K}` }[StringKeyOf<T>]
: '';
function createJsonPointer<T extends object>(key: StringKeyOf<T>): JsonPointer<T & object> {
return `/${key}`;
} There's an existing issue tracking the fact that you have to write the meaningless " |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Following error is emitted because
StringKeyOf<T>
is falsely widened tostring
:π Expected behavior
I expected the code to type check as it does if the function's type parameter is replaced with a concrete type.
The text was updated successfully, but these errors were encountered: