-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Describing an Interface where unknown properties are of a specific type #20597
Comments
Intersection types allow this type Foo =
{
name: string;
age: number;
} & {
[prop: string]: string;
}; |
@alexrock - Thanks for letting me know. Learnt something new today. I tried the following type Foo = {
name: string;
age: number;
} & {
[prop: string]: string;
};
var f: Foo = {
name: "foo",
age: 10,
color: "red"
}; and it still gives an error:
NOTE: I also found some info at @basarat's TS gitbook which explains why all members must conform to the index signature. |
@amarzavery - I understand. Object literals don't allow this, but member access is still allowed and correctly typed. type Foo =
{
name: string
age: number
} & {
[prop: string]: string
}
let foo: Foo = JSON.parse(`{
"name": "foo",
"age": 10,
"color": "red"
}`)
foo.color = 0x0000ff // type error
foo.color = 'blue' // ok, color is string I can't say, absolutely, if this is the intended behavior. |
Looks like a potential duplicate of #17867 (partially discussed here #18394 (comment)). |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
We would like to model an
Interface
in TS for the above json schema definition as follows:However, TS gives the following error:
any
The above approach would be perfectly fine for
"additionalProperties": true
or"additionalProperties": { "type": "object" }
, but not for specific types.Dictionary<string>
.This would be convenient for C#, Python, etc. developers (as they deal with objects in that language). However, it would be inconvenient for JS developers to provide (or access) a JSON object that has unknown properties in a property bag called additionalProperties.
Any help/idea in solving this problem will be very helpful.
The text was updated successfully, but these errors were encountered: