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
Add a new type (stringliteral in the example below) that would be a super-type of all string literals, but not unions.
Use Cases
Mapped types are great when building an object out of another object, or when starting with object literals with constant property names. There currently doesn't seem to be an option to build out an object typed with mapped types with variable property names.
Examples
We can build an algebra of base object construction and composition functions.
Note: this example composes plain objects, but the actual motivation is to have more complex structures that internally maintain a typed object and the algebra allows composing them in various ways.
The current behavior is of course correct, in that { [n]: V } can be implicitly cast only to { [P in N]?: V } - if N is a union, the other members of the union don't have a value set.
What could help is to narrow the generic argument N to a single string literal to prevent unions:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Search Terms
Suggestion
Add a new type (
stringliteral
in the example below) that would be a super-type of all string literals, but not unions.Use Cases
Mapped types are great when building an object out of another object, or when starting with object literals with constant property names. There currently doesn't seem to be an option to build out an object typed with mapped types with variable property names.
Examples
We can build an algebra of base object construction and composition functions.
Note: this example composes plain objects, but the actual motivation is to have more complex structures that internally maintain a typed object and the algebra allows composing them in various ways.
But this is unsafe -
singlePropertyObject
can be used to create arbitrary objects with non-optional fields but without value without type errors:The current behavior is of course correct, in that
{ [n]: V }
can be implicitly cast only to{ [P in N]?: V }
- ifN
is a union, the other members of the union don't have a value set.What could help is to narrow the generic argument
N
to a single string literal to prevent unions:Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: