JSON-typescript Basic Type Definitions
This package provides fundamental TypeScript type definitions specifically for JSON objects. Its primary purpose is to enable compile-time validation of JSON structures, ensuring that data conforms to expected shapes like `JSONValue` or `JSONObject`. The package is currently at version 1.1.2, with its last release in March 2019, indicating an inactive development cadence. Unlike more advanced libraries that generate JSON schemas from TypeScript types (e.g., `json-schema-to-typescript`) or provide runtime object mapping (e.g., `json2typescript`), `json-typescript` focuses solely on offering basic structural types for static analysis. It's suitable for projects needing straightforward type enforcement for incoming or outgoing JSON data at compile time.
Common errors
-
SyntaxError: Unexpected token {cause Using the incorrect import syntax `import * as { ... } from 'json-typescript';` shown in the package's README, which is not valid JavaScript or TypeScript for named imports.fixFor named type imports, use `import type { Value, Object } from 'json-typescript';` or `import type { Value as JSONValue, Object as JSONObject } from 'json-typescript';`. -
Cannot find module 'json-typescript' or its corresponding type declarations.
cause Attempting to import the package in a CommonJS context or without proper `tsconfig.json` setup for module resolution, especially with older TypeScript versions or when `esModuleInterop` is not enabled.fixEnsure `tsconfig.json` has `"module": "esnext"` (or compatible), `"moduleResolution": "node"`, and `"esModuleInterop": true`. If using CommonJS, direct `require` might not provide type inference and `import` should still be preferred for type safety. Reinstall `@types/json-typescript` if it were a separate types package (not applicable here as it ships its own types).
Warnings
- gotcha The `json-typescript` package has not been updated since March 2019. Its type definitions may not be fully compatible with newer TypeScript versions (e.g., TypeScript 4.x, 5.x) or may lack support for modern ECMAScript/JSON features and stricter type-checking configurations. This could lead to unexpected type errors or less precise type inference compared to actively maintained alternatives.
- deprecated The README contains an incorrect import syntax example: `import * as { Value as JSONValue, Object as JSONObject } from 'json-typescript';`. This is syntactically invalid for named type imports in TypeScript.
Install
-
npm install json-typescript -
yarn add json-typescript -
pnpm add json-typescript
Imports
- All types as namespace
const _JSON = require('json-typescript');import * as _JSON from 'json-typescript';
- Value
import { Value } from 'json-typescript'; // Older TS or mixing type and value imports import * as { Value } from 'json-typescript'; // Incorrect syntaximport type { Value } from 'json-typescript'; - Object
import { Object as JSONObject } from 'json-typescript'; // Older TS or mixing type and value imports import * as { Object as JSONObject } from 'json-typescript'; // Incorrect syntaximport type { Object as JSONObject } from 'json-typescript';
Quickstart
npm install --save-dev json-typescript
// tsconfig.json might need:
// {
// "compilerOptions": {
// "moduleResolution": "node",
// "esModuleInterop": true
// }
// }
import type { Value as JSONValue, Object as JSONObject } from 'json-typescript';
// ✅ This should be OK: a valid JSON structure
let validDoc: JSONValue = {
data: {
type: 'articles',
id: '1',
attributes: {
title: 'My Article',
content: 'Hello World!'
}
},
meta: {
timestamp: new Date().toISOString()
}
};
// ⛔️ This should NOT be OK (functions are not allowed in JSONValue)
// let invalidDocWithFunction: JSONValue = {
// foo() { return 'bar'; }
// };
// ⛔️ This should NOT be OK (Array is not a JSONObject, it's a JSONValue)
// let invalidDocAsArray: JSONObject = [];
// Correct usage of JSONObject:
let myJsonObject: JSONObject = {
key1: 'value1',
key2: 123,
key3: true,
key4: null,
key5: { nested: 'object' },
key6: ['array', 456]
};
console.log('Valid JSON Document:', validDoc);
console.log('My JSON Object:', myJsonObject);