What is quicktype-core?
The quicktype-core npm package is a powerful tool for generating type-safe code from JSON, GraphQL, and other data formats. It helps developers quickly create models and types in various programming languages, ensuring that the data structures are correctly typed and reducing the likelihood of runtime errors.
What are quicktype-core's main functionalities?
Generate TypeScript Interfaces from JSON
This feature allows you to generate TypeScript interfaces from a given JSON string. It ensures that the JSON data is correctly typed in TypeScript, making it easier to work with the data in a type-safe manner.
const quicktype = require('quicktype-core');
const { InputData, JSONInput, quicktypeJSON } = quicktype;
async function generateTypeScript(jsonString) {
const jsonInput = new JSONInput(new quicktype.JSONSchemaStore());
await jsonInput.addSource({ name: 'MyType', samples: [jsonString] });
const inputData = new InputData();
inputData.addInput(jsonInput);
const result = await quicktypeJSON({
inputData,
lang: 'ts'
});
return result.lines.join('\n');
}
const jsonString = '{ "name": "John", "age": 30 }';
generateTypeScript(jsonString).then(console.log);
Generate C# Classes from JSON
This feature allows you to generate C# classes from a given JSON string. It ensures that the JSON data is correctly typed in C#, making it easier to work with the data in a type-safe manner.
const quicktype = require('quicktype-core');
const { InputData, JSONInput, quicktypeJSON } = quicktype;
async function generateCSharp(jsonString) {
const jsonInput = new JSONInput(new quicktype.JSONSchemaStore());
await jsonInput.addSource({ name: 'MyType', samples: [jsonString] });
const inputData = new InputData();
inputData.addInput(jsonInput);
const result = await quicktypeJSON({
inputData,
lang: 'csharp'
});
return result.lines.join('\n');
}
const jsonString = '{ "name": "John", "age": 30 }';
generateCSharp(jsonString).then(console.log);
Generate JSON Schema from TypeScript Interfaces
This feature allows you to generate JSON Schema from TypeScript interfaces. It ensures that the TypeScript data structures are correctly represented in JSON Schema, making it easier to validate JSON data against the schema.
const quicktype = require('quicktype-core');
const { InputData, TypeScriptInput, quicktypeJSON } = quicktype;
async function generateJSONSchema(tsString) {
const tsInput = new TypeScriptInput(new quicktype.JSONSchemaStore());
await tsInput.addSource({ name: 'MyType', samples: [tsString] });
const inputData = new InputData();
inputData.addInput(tsInput);
const result = await quicktypeJSON({
inputData,
lang: 'schema'
});
return result.lines.join('\n');
}
const tsString = 'interface MyType { name: string; age: number; }';
generateJSONSchema(tsString).then(console.log);
Other packages similar to quicktype-core
json-schema-to-typescript
The json-schema-to-typescript package converts JSON Schemas to TypeScript interfaces. It is similar to quicktype-core in that it helps generate type-safe code, but it focuses specifically on JSON Schema to TypeScript conversion.
typescript-json-schema
The typescript-json-schema package generates JSON Schema from your TypeScript sources. It is similar to quicktype-core in that it helps create JSON Schemas from TypeScript, but it does not support as many languages or input formats as quicktype-core.
swagger-typescript-api
The swagger-typescript-api package generates TypeScript API client code from Swagger/OpenAPI definitions. It is similar to quicktype-core in that it generates type-safe TypeScript code, but it focuses specifically on API client generation from Swagger/OpenAPI definitions.