Here is an extension related to the topic of Typescript: interface that extends a JSON type
Consider the following JSON type:
type JSONValue =
| string
| number
| boolean
| null
| JSONValue[]
| {[key: string]: JSONValue}
The goal is to inform typescript that interfaces matching the JSONType should be automatically casted when passed into functions accepting the JSONType. For instance:
interface Foo {
name: 'FOO',
fooProp: string
}
const bar = (foo: Foo) => { return foo }
const wrap = (fn: (...args: JSONValue[]) => JSONValue, args: JSONValue[]) => {
return fn(...args);
}
wrap(bar, {name: 'FOO', fooProp: 'hello'});
However, this currently results in an error stating:
Argument of type '(foo: Foo) => Foo' is not assignable to parameter of type '(...args: JSONValue[]) => JSONValue'.
Types of parameters 'foo' and 'args' are incompatible.
Type 'JSONValue' is not assignable to type 'Foo'.
Type 'null' is not assignable to type 'Foo'.
even though logically we know that the inputted foo adheres to JSON standards.
You can play around with this code on the TypeScript Playground here
Is there a way for typescript to acknowledge that this interface is indeed a valid JSON type?