Hi,
JSON is an important data exchange format. At present there is no explicit way to annotate an object as pure JSON. We were expecting union types to solve this problem via the following:
interface Json {
[x: string]: string|number|boolean|Date|Json|JsonArray;
}
interface JsonArray extends Array<string|number|boolean|Date|Json|JsonArray> { }
There are currently a number of problems with this.
class Bass {
f(): Id{ return undefined;}
}
// Error: missing index signature
class Foo extends Bass {
f() { return { id: 10 }}
}
interface Foo extends Json {
foo: { val: string }; // Error: Not assignable
}
-
Other problems:
// Error: Missing index signature
var result: Id[] = 'a|b'.split('|').map(item => {
return { id: 0 };
});
The first two problems look likely to be resolved, but not the last two.
This motivates the introduction of a natively supported json type that conforms to the JSON spec.
It should be possible to derive a custom JSON object from the json type with non of the problems above. Furthermore, since JSON is a data exchange format it should be possible to mark fields as nullable on types deriving from json (which would trigger a type guard).
I am of course aware that this is all rather vague!
Hi,
JSON is an important data exchange format. At present there is no explicit way to annotate an object as pure JSON. We were expecting union types to solve this problem via the following:
There are currently a number of problems with this.
Contextual typing is absent in parenthetic expression Consider contextually typing parenthesized expressions #920
Contextual typing does not flow from base to subclasses Consider contextually typing class member functions by their base class/interface members #1373
Other problems:
The first two problems look likely to be resolved, but not the last two.
This motivates the introduction of a natively supported
jsontype that conforms to the JSON spec.It should be possible to derive a custom JSON object from the
jsontype with non of the problems above. Furthermore, since JSON is a data exchange format it should be possible to mark fields as nullable on types deriving fromjson(which would trigger a type guard).I am of course aware that this is all rather vague!