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
muslnode 18–226 runs
build_error
glibcnode 18–226 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
}
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.
fixEnsure 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.
fixWhen 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`).
fixInstall `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`.
Audit
Dependencies
No dependency data recorded yet.