-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Specific type revert to string with dynamic key #60896
Comments
This is working as intended. Duplicate of #53912. This call is perfectly valid, but your functions would return incomplete objects: example1<"a" | "b">("a", "value"); |
Thanks. I understand the cause of the issue now. But we can't do things like function example(key: infer K satisfies string, value: unknown): { [key in K]: unknown }
{
return { [key]: value };
} Or function example<K extends unique string>(key: K, value: unknown): { [key in K]: unknown }
{
return { [key]: value };
} These syntax do not exists. |
You can't do that because it's simply not supported (yet?). There's an open issue for this: #27808 |
Though it appear similar, it's not exactly the same. |
It is exactly the same. You want to limit the type argument to a single value, disallowing unions.
It would still need to be a generic type, and generic types can be provided by the caller explicitly. |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
In this example it wouldn't be possible for the caller to provide the type explicitly. function example(key: infer K satisfies string, value: unknown): { [key in K]: unknown }
{
return { [key]: value };
} |
Only because it's arbitrary syntax with nothing behind it. When implemented the argument would still need to be generic. And you could still always do this: declare const myKey: "a" | "b"
example(myKey, value); And according to the type system the object would need to have two properties. Again, what you want is #27808. |
π Search Terms
dynamic key type
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.7.2#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgRgB4BpFVKZMAEwGdFaoAnGMAcwD4AKAa2QE8AXIhIAaRADd0uEMmHgeYOAHcwASmEBvRAG0+-RKxEBdeWEUqkAXwBQmmwEgmyKCCZJtegacnTZiKwBuG1sbUEhYBHIsPGQAJlJyShp6RhZ2bn1hMV8ZOUQFJVUNRAAlZAg4JmpScULLDjtHZ1d3RE99Hyk8gOCrIA
π» Code
π Actual behavior
The type of the key is K but reverts to string
π Expected behavior
The type of the key is preserved as K.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: