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
I think it should be possible to have fixed properties on a dictionary that doesn't need to match the type of the generic ones, or the other way round: that generic values should not be a superset of the fixed values
🔍 Search Terms
dynamic keys, static keys, mixing static and dynamic keys
✅ Viability Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
So, in terms of code, I want to be able to type this:
typeX={startDate: string,[name: string]: number}
However, typescript does not understand that the fixedKey startDate doesn't need to match the other generic key value pair, so you are forced to write it like this:
Adding dynamic keys to objects in javascript is quite common, and they don't need to match the type of existing keys. Many times you have a set of well known keys mixed with other dynamic keys, and you only care about the fixed ones and their type.
With destructuring is quite easy taking apart the known keys from the dynamic ones
So having all the "otherKeys" object have a mixed type just because fixedKey and knownKey didn't matched the type of the dynamic keys is an unnecessary trouble.
The text was updated successfully, but these errors were encountered:
Indexed types with known properties are already unsound, and your suggestion would just worsen the situation by introducing the potential errors to not only the known properties, but to any properties accessed with the indexer.
const x: X = { startDate: "example" };
const key: string = "startDate";
x[key] = 123;
x.startDate; // Compile time type is string, runtime type is number;
But that is something the compiler should be able to catch. You already have the startDate as a constant string so it knows that you are trying to add an invalid type to a static defined prop.
But I see that, for some runtime values, you have no way to know if a string contains startDate or not.
Yes, it is a duplicate of those.
Sadly you can't use an intersection type for this. Sure, you can declare it, but you can not type anything with that value.
Suggestion
I think it should be possible to have fixed properties on a dictionary that doesn't need to match the type of the generic ones, or the other way round: that generic values should not be a superset of the fixed values
🔍 Search Terms
dynamic keys, static keys, mixing static and dynamic keys
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
So, in terms of code, I want to be able to type this:
However, typescript does not understand that the fixedKey startDate doesn't need to match the other generic key value pair, so you are forced to write it like this:
💻 Use Cases
Adding dynamic keys to objects in javascript is quite common, and they don't need to match the type of existing keys. Many times you have a set of well known keys mixed with other dynamic keys, and you only care about the fixed ones and their type.
With destructuring is quite easy taking apart the known keys from the dynamic ones
So having all the "otherKeys" object have a mixed type just because fixedKey and knownKey didn't matched the type of the dynamic keys is an unnecessary trouble.
The text was updated successfully, but these errors were encountered: