Registry / type-stubs / json-typescript

json-typescript

JSON →
library1.1.2jsnpmunverified

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.

npm install json-typescript
INSTALL
IMPORT
SIG · JSON-TYPESCRIPT
J
json-typescript
type-stubsjavascriptv1.1.2
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.

All types as namespace
import * as _JSON from 'json-typescript';
const _JSON = require('json-typescript');
This imports all type definitions under the `_JSON` namespace. Ensure your TypeScript project is configured for ESM if encountering issues with CommonJS `require`.
Value
import type { Value } from 'json-typescript';
import { Value } from 'json-typescript'; // Older TS or mixing type and value imports import * as { Value } from 'json-typescript'; // Incorrect syntax
Use the `import type` syntax for explicitly importing only type definitions, which is a best practice in modern TypeScript to avoid bundling unnecessary runtime code.
Object
import type { Object as JSONObject } from 'json-typescript';
import { Object as JSONObject } from 'json-typescript'; // Older TS or mixing type and value imports import * as { Object as JSONObject } from 'json-typescript'; // Incorrect syntax
Aliasing types like `Object` to `JSONObject` is common to prevent naming conflicts with JavaScript's global `Object`. Use `import type` for explicit type-only imports.

Demonstrates installation and basic usage of JSON-typescript for compile-time validation of JSON structures, highlighting correct and incorrect type assignments.

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);
Debug
Known issues
gotchaThe `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.
fix
For new projects or those requiring robust JSON schema validation or runtime type checking, consider actively maintained alternatives like `json-schema-to-typescript`, `typescript-json-schema` for schema generation, or `json2typescript` for runtime deserialization. If continuing to use this package, thoroughly test compatibility with your TypeScript version.
affects: >=1.1.2
deprecatedThe 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.
fix
Always use `import type { Value, Object } from 'json-typescript';` for named type imports, or `import type { Value as JSONValue, Object as JSONObject } from 'json-typescript';` if aliasing. The `import * as ...` syntax is for namespace imports, not for destructuring named exports.
affects: >=1.0.0
Errors
Common errors & fixes
SyntaxError: Unexpected token {
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.
fix
For 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.
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.
fix
Ensure `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).
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
30 hits · last 30 days
node
28
OpenAI (training)
1
Resources
json-typescript — npm install json-typescript · libregistry