Skip to content

Conversation

@Tiberiu02
Copy link

When using the typeOf keyword, type definitions of all functions will now have a "parameters" field containing a list of the type definitions of all function parameters.

This is very useful for fully automated runtime type checking in RPC architectures.

Example:

Code:
(Note the use of various function syntaxes)

function sum(a: number, b: number) { return a + b; } function prod(a: number, b: number) { return a + b; } const length = (s: string) => s.length; const extractAttribute = (obj: object, attribute: string) => obj[attribute]; class Arrays { static get(array: number[], index: number) { return array[index]; } } export const API = { numbers: { sum, prod }, objects: { length, extractAttribute, extractArrayEl: Arrays.get } }; 

Produced schema:
(note the parameters lists)

{ "type": "object", "properties": { "api": { "type": "object", "properties": { "numbers": { "type": "object", "properties": { "sum": { "typeof": "function", "parameters": [ { "type": "number" }, { "type": "number" } ] }, "prod": { "typeof": "function", "parameters": [ { "type": "number" }, { "type": "number" } ] } } }, "objects": { "type": "object", "properties": { "length": { "typeof": "function", "parameters": [ { "type": "string" } ] }, "extractAttribute": { "typeof": "function", "parameters": [ { "type": "object", "properties": {}, "additionalProperties": true }, { "type": "string" } ] }, "extractArrayEl": { "typeof": "function", "parameters": [ { "type": "array", "items": { "type": "number" } }, { "type": "number" } ] } } } } } }, "$schema": "http://json-schema.org/draft-07/schema#" } 
@Tiberiu02
Copy link
Author

@YousefED Do you think you could you take a look over this? It would be very useful for our project

@domoritz
Copy link
Collaborator

How does this compare with what ts-json-schema-generator does?

@Tiberiu02
Copy link
Author

Didn't know about ts-json-schema-generator, let me take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants