Registry / type-stubs / ts-simple-type

ts-simple-type

JSON →
library1.0.7jsnpmunverified

ts-simple-type is a TypeScript utility library (current stable version 1.0.7, last published July 2020) designed to provide essential helper functions for analyzing and comparing TypeScript types. It aims to bridge gaps in the native TypeScript compiler API, particularly for direct assignability checks and programmatically constructing types, as noted by discussions in TypeScript GitHub issues #9879 and #29432. The library works by converting native TypeScript `ts.Type` objects into its own `SimpleType` interface, which offers a more standardized and easily serializable representation. This `SimpleType` abstraction facilitates advanced type analysis, comparison, and even serialization for use in various environments, including browsers. A key differentiator is its extensive test suite, comprising over 35,000 tests that validate its type-checking results against actual TypeScript diagnostics. Despite its usefulness, the package shows a slower release cadence, with the last update in 2020, suggesting it is now in a maintenance state rather than active development.

npm install ts-simple-type
INSTALL
IMPORT
SIG · TS-SIMPLE-TYPE
T
ts-simple-type
type-stubsjavascriptv1.0.7
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.

isAssignableToType
import { isAssignableToType } from 'ts-simple-type';
const isAssignableToType = require('ts-simple-type').isAssignableToType;
This function is a named export. It often requires a `ts.TypeChecker` instance as its third argument.
toSimpleType
import { toSimpleType } from 'ts-simple-type';
import toSimpleType from 'ts-simple-type';
Converts a native TypeScript `ts.Type` into the library's `SimpleType` representation. It's a named export and also commonly requires a `ts.TypeChecker`.
SimpleType
import { SimpleType } from 'ts-simple-type';
import { Type } from 'ts-simple-type';
This is the core interface for the library's internal type representation. It is a named export, distinct from `ts.Type` from the TypeScript API.
typeToString
import { typeToString } from 'ts-simple-type';
Utility function to get a string representation of a `SimpleType`. Renamed from `toTypeString` in v1.0.0.

This quickstart demonstrates how to set up a basic TypeScript program to obtain a `ts.TypeChecker`, extract types from code, and then use `ts-simple-type`'s `isAssignableToType` function to check type compatibility. It also shows conversion to `SimpleType` and direct usage of `SimpleType` objects. Requires `typescript` package to be installed as a peer dependency.

import ts from 'typescript'; import { isAssignableToType, toSimpleType, typeToString, SimpleType } from 'ts-simple-type'; // 1. Set up a minimal TypeScript program to get a TypeChecker const code = ` type MyNumber = number; let a: MyNumber = 10; let b: string = "hello"; let d: string | number = 5; `; const fileName = 'test-file.ts'; const sourceFile = ts.createSourceFile(fileName, code, ts.ScriptTarget.ES2015, true); const host: ts.LanguageServiceHost = { getScriptFileNames: () => [fileName], getScriptVersion: (f) => '1', getScriptSnapshot: (f) => { if (f === fileName) { return ts.ScriptSnapshot.fromString(code); } return undefined; }, getCurrentDirectory: () => '/', getCompilationSettings: () => ({}), getDefaultLibFileName: ts.getDefaultLibFileName, fileExists: (f) => f === fileName, readFile: (f) => (f === fileName ? code : undefined), readDirectory: () => [], }; const program = ts.createProgram([fileName], {}, host); const typeChecker = program.getTypeChecker(); // 2. Extract TypeScript types from the AST const typeAliases = sourceFile.statements.filter(ts.isTypeAliasDeclaration); const varStatements = sourceFile.statements.filter(ts.isVariableStatement); const myNumberType = typeChecker.getTypeFromTypeNode(typeAliases[0].type); // Type of 'MyNumber' const aSymbol = typeChecker.getSymbolAtLocation(varStatements[0].declarationList.declarations[0].name); const aType = aSymbol ? typeChecker.getTypeOfSymbolAtLocation(aSymbol, aSymbol.valueDeclaration!) : undefined; // Type of 'a' const bSymbol = typeChecker.getSymbolAtLocation(varStatements[1].declarationList.declarations[0].name); const bType = bSymbol ? typeChecker.getTypeOfSymbolAtLocation(bSymbol, bSymbol.valueDeclaration!) : undefined; // Type of 'b' const dSymbol = typeChecker.getSymbolAtLocation(varStatements[2].declarationList.declarations[0].name); const dType = dSymbol ? typeChecker.getTypeOfSymbolAtLocation(dSymbol, dSymbol.valueDeclaration!) : undefined; // Type of 'd' if (aType && bType && dType) { // 3. Use ts-simple-type to check assignability and convert types console.log(`Is '${typeToString(toSimpleType(aType, typeChecker))}' assignable to 'number'? ${isAssignableToType(aType, myNumberType, typeChecker)}`); // true console.log(`Is '${typeToString(toSimpleType(bType, typeChecker))}' assignable to 'number'? ${isAssignableToType(bType, myNumberType, typeChecker)}`); // false console.log(`Is '${typeToString(toSimpleType(dType, typeChecker))}' assignable to 'number'? ${isAssignableToType(dType, myNumberType, typeChecker)}`); // true // Demonstrate working with SimpleType directly const customStringType: SimpleType = { kind: "STRING" }; const customNumberType: SimpleType = { kind: "NUMBER" }; console.log(`Is '${typeToString(toSimpleType(bType, typeChecker))}' assignable to custom STRING type? ${isAssignableToType(bType, customStringType, typeChecker)}`); // true console.log(`Is '${typeToString(toSimpleType(aType, typeChecker))}' assignable to custom NUMBER type? ${isAssignableToType(aType, customNumberType, typeChecker)}`); // true }
Debug
Known issues
breaking`toTypeString` function was renamed to `typeToString`.
fix
Update calls from `toTypeString(...)` to `typeToString(...)`.
affects: >=1.0.0
breaking`simpleTypeToString` is no longer exported.
fix
Migrate to `typeToString` for string representation of types.
affects: >=1.0.0
breakingThe `spread` property on `SimpleTypeFunctionParameter` was renamed to `rest`.
fix
Update property access from `.spread` to `.rest` on `SimpleTypeFunctionParameter` objects.
affects: >=1.0.0
breakingThe `hasRestElement` property on `SimpleTypeTuple` was renamed to `rest`.
fix
Update property access from `.hasRestElement` to `.rest` on `SimpleTypeTuple` objects.
affects: >=1.0.0
breaking`SimpleTypeKind` and `SimpleTypeModifierKind` were converted from enums to string literal unions.
fix
If using these kinds directly in type comparisons or switch statements, ensure they are compared against string literals (e.g., `type.kind === 'STRING'` instead of `type.kind === SimpleTypeKind.STRING`).
affects: >=1.0.0
breakingThe `methods` and `properties` properties on `SimpleTypeClass` were renamed to `members`.
fix
Access class members via the `.members` property instead of `.methods` or `.properties`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'kind') or Argument of type 'undefined' is not assignable to parameter of type 'SimpleTypeKind'.
Attempting to use a type function (e.g., `isAssignableToType`) without providing a valid `ts.Type` or `SimpleType` object, or an incorrect `SimpleType` structure.
fix
Ensure that the `ts.Type` or `SimpleType` arguments passed to functions like `isAssignableToType` are not `undefined` and conform to the expected interface, especially if constructing `SimpleType` objects manually.
Error: Parameter 'typeChecker' is required when giving a Typescript Type to the function.
Functions like `isAssignableToType` and `toSimpleType` are called with a native `ts.Type` object but without providing a `ts.TypeChecker` instance.
fix
When working with `ts.Type` objects, always pass the `ts.TypeChecker` instance (obtained from `program.getTypeChecker()`) as the third argument to the `ts-simple-type` functions that require it.
TS2304: Cannot find name 'ts'.
The `typescript` package is not imported or installed when attempting to use its API (e.g., `ts.createProgram`, `ts.ScriptTarget`).
fix
Install `typescript` as a development dependency (`npm install typescript --save-dev`) and import it (`import ts from 'typescript';`) where its API is used, especially for setting up the `TypeChecker`.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources