Registry / serialization / quicktype-typescript-input

quicktype-typescript-input

JSON →
library23.2.6jsnpmunverified

The `quicktype-typescript-input` package serves as an input module for the `quicktype` code generation tool, specifically enabling the use of TypeScript source code as a schema definition. `quicktype` itself is a robust utility that generates strongly-typed models and serializers from various inputs like JSON, JSON Schema, TypeScript, and GraphQL queries. This package allows developers to define their data structures using TypeScript interfaces or types, and then generate client libraries or data models in numerous target programming languages, including C#, Go, Rust, Java, Swift, Python, and even TypeScript itself with runtime validation. Currently, the package is at version `23.2.6`, aligning with the main `quicktype` project's stable releases. Its primary differentiator is simplifying the process of creating type-safe data access layers by automating boilerplate code, allowing developers to focus on application logic rather than manual data model definition and deserialization.

npm install quicktype-typescript-input
INSTALL
IMPORT
SIG · QUICKTYPE-TYPESCRI
Q
quicktype-typescript-input
serializationjavascriptv23.2.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

quicktype
import { quicktype, InputData } from 'quicktype-core';
import quicktype from 'quicktype';
The core quicktype function and InputData are part of 'quicktype-core', not directly 'quicktype-typescript-input'.
TypeScriptInput
import { TypeScriptInput } from 'quicktype-typescript-input';
import { TypeScriptInput } from 'quicktype-core';
This package exports the specific input class for TypeScript source.
QuicktypeOptions
import { QuicktypeOptions } from 'quicktype-core';
For type-checking quicktype configuration options.

This quickstart demonstrates how to programmatically use `quicktype-typescript-input` to take a TypeScript source string defining interfaces and enums, and then generate corresponding type definitions in different target languages like TypeScript or C# using the `quicktype-core` library.

import { quicktype, InputData } from 'quicktype-core'; import { TypeScriptInput } from 'quicktype-typescript-input'; async function generateTypesFromTypeScript(typescriptSource: string, targetLanguage: string, typeName: string): Promise<string> { const tsInput = new TypeScriptInput(); await tsInput.addSource({ name: typeName, samples: new Set([typescriptSource]) }); const inputData = new InputData(); inputData.addInput(tsInput); const { lines } = await quicktype({ inputData, lang: targetLanguage, typeName, rendererOptions: { 'just-types': 'true' // Common option to only generate types, no serialization code } }); return lines.join('\n'); } const myTypeScriptSource = ` interface User { id: string; name: string; email: string; isActive: boolean; createdAt: Date; } enum UserRole { Admin = 'admin', Editor = 'editor', Viewer = 'viewer', } `; // Example 1: Generate TypeScript interfaces from TypeScript source generateTypesFromTypeScript(myTypeScriptSource, 'typescript', 'UserAndRole') .then(output => { console.log('Generated TypeScript:\n', output); }) .catch(error => console.error('Error generating TypeScript:', error)); // Example 2: Generate C# classes from TypeScript source generateTypesFromTypeScript(myTypeScriptSource, 'csharp', 'UserAndRole') .then(output => { console.log('\nGenerated C#:\n', output); }) .catch(error => console.error('Error generating C#:', error));
Debug
Known issues
gotchaWhen using `quicktype` programmatically or via CLI, type names might sometimes be unpredictably renamed, especially when inferring types from complex schemas or multiple samples. This can lead to inconsistent generated code and requires manual inspection or explicit `--top-level` naming.
fix
Specify the top-level type name explicitly using the `--top-level NAME` CLI option or the `typeName` option in programmatic usage to ensure consistent naming. Review generated code for unexpected renamings.
affects: >=20.0.0
breakingOlder versions of the `quicktype` web interface or cached preferences might store language IDs with capitalization (e.g., 'TypeScript'). Newer versions of `quicktype` expect lowercase language IDs (e.g., 'typescript'), which can cause the application to fail loading due to an 'Unknown language name' error.
fix
Clear browser cache and local storage for `app.quicktype.io` or specifically clear the `localStorage` entry for `quicktype` preferences. This forces the application to re-initialize with correct lowercase language IDs.
affects: >=23.0.0
gotchaUsing `additionalProperties: false` at the root of a JSON Schema in conjunction with `allOf` can lead to unexpected type generation results, as `quicktype` (and JSON Schema validators in general) process these keywords in a way that might not align with intuitive merging behavior.
fix
Carefully review the JSON Schema. If `additionalProperties: false` is necessary, ensure that all expected properties, including those from `allOf` subschemas, are explicitly defined or allowed at the root or within the merged schema. Consider removing `additionalProperties: false` if strictness is not paramount or if it conflicts with schema composition.
affects: >=20.0.0
deprecatedGenerating code from TypeScript input via `quicktype` is still considered 'Experimental' according to the main quicktype GitHub README. This implies that the feature might undergo significant changes, could have limitations, or may not be as stable as JSON or JSON Schema inputs.
fix
Be aware of potential instabilities or breaking changes. For mission-critical applications, consider converting TypeScript definitions to JSON Schema first, or thoroughly test the generated code for correctness and maintainability. Monitor quicktype's official documentation for updates on this feature's status.
affects: >=20.0.0
Errors
Common errors & fixes
Error: No inputs were found in config file 'tsconfig.json'
This is a general TypeScript error, often seen when `tsconfig.json`'s `include` or `files` array doesn't match any actual TypeScript files in the project, which can occur during initial setup with `quicktype-typescript-input`.
fix
Ensure your `tsconfig.json`'s `include` or `files` array correctly points to your TypeScript source files, or create at least one `.ts` file matched by the configuration.
Property 'X' is missing in type 'Y' but required in type 'Z'.
Generated TypeScript types are strict by default. This error indicates that an object or value does not conform to the expected interface or type, commonly due to discrepancies between the input data/schema used for generation and the actual data being processed.
fix
Adjust the input TypeScript source or JSON Schema to accurately reflect all expected properties, including optionality. Alternatively, modify the consuming code to provide all required properties or handle optional properties correctly (e.g., with null checks).
Uncaught (in promise) Error: Unknown language name: TypeScript
This error occurs in the quicktype web application or similar environments when a stored preference for a target language uses an outdated or incorrect capitalization (e.g., 'TypeScript' instead of 'typescript').
fix
Clear browser data, specifically local storage, for the `quicktype.io` domain. This will reset language preferences to their default, correctly capitalized values.
Upgrade
Version history
23.2.6latest on npm
Audit
Dependencies
quicktype-corerequiredProvides the core quicktype function and InputData class for programmatic use.
typescriptrequiredRequired for parsing TypeScript input definitions.
Agent activity
11 hits · last 30 days
node
10
Resources