{"id":12187,"library":"ts-simple-type","title":"TypeScript Simple Type Checker","description":"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.","status":"maintenance","version":"1.0.7","language":"javascript","source_language":"en","source_url":"https://github.com/runem/ts-simple-type","tags":["javascript","typescript","ast","typechecker","type"],"install":[{"cmd":"npm install ts-simple-type","lang":"bash","label":"npm"},{"cmd":"yarn add ts-simple-type","lang":"bash","label":"yarn"},{"cmd":"pnpm add ts-simple-type","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This function is a named export. It often requires a `ts.TypeChecker` instance as its third argument.","wrong":"const isAssignableToType = require('ts-simple-type').isAssignableToType;","symbol":"isAssignableToType","correct":"import { isAssignableToType } from 'ts-simple-type';"},{"note":"Converts a native TypeScript `ts.Type` into the library's `SimpleType` representation. It's a named export and also commonly requires a `ts.TypeChecker`.","wrong":"import toSimpleType from 'ts-simple-type';","symbol":"toSimpleType","correct":"import { toSimpleType } from 'ts-simple-type';"},{"note":"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.","wrong":"import { Type } from 'ts-simple-type';","symbol":"SimpleType","correct":"import { SimpleType } from 'ts-simple-type';"},{"note":"Utility function to get a string representation of a `SimpleType`. Renamed from `toTypeString` in v1.0.0.","symbol":"typeToString","correct":"import { typeToString } from 'ts-simple-type';"}],"quickstart":{"code":"import ts from 'typescript';\nimport { isAssignableToType, toSimpleType, typeToString, SimpleType } from 'ts-simple-type';\n\n// 1. Set up a minimal TypeScript program to get a TypeChecker\nconst code = `\n  type MyNumber = number;\n  let a: MyNumber = 10;\n  let b: string = \"hello\";\n  let d: string | number = 5;\n`;\n\nconst fileName = 'test-file.ts';\nconst sourceFile = ts.createSourceFile(fileName, code, ts.ScriptTarget.ES2015, true);\n\nconst host: ts.LanguageServiceHost = {\n  getScriptFileNames: () => [fileName],\n  getScriptVersion: (f) => '1',\n  getScriptSnapshot: (f) => {\n    if (f === fileName) {\n      return ts.ScriptSnapshot.fromString(code);\n    }\n    return undefined;\n  },\n  getCurrentDirectory: () => '/',\n  getCompilationSettings: () => ({}),\n  getDefaultLibFileName: ts.getDefaultLibFileName,\n  fileExists: (f) => f === fileName,\n  readFile: (f) => (f === fileName ? code : undefined),\n  readDirectory: () => [],\n};\n\nconst program = ts.createProgram([fileName], {}, host);\nconst typeChecker = program.getTypeChecker();\n\n// 2. Extract TypeScript types from the AST\nconst typeAliases = sourceFile.statements.filter(ts.isTypeAliasDeclaration);\nconst varStatements = sourceFile.statements.filter(ts.isVariableStatement);\n\nconst myNumberType = typeChecker.getTypeFromTypeNode(typeAliases[0].type); // Type of 'MyNumber'\n\nconst aSymbol = typeChecker.getSymbolAtLocation(varStatements[0].declarationList.declarations[0].name);\nconst aType = aSymbol ? typeChecker.getTypeOfSymbolAtLocation(aSymbol, aSymbol.valueDeclaration!) : undefined; // Type of 'a'\n\nconst bSymbol = typeChecker.getSymbolAtLocation(varStatements[1].declarationList.declarations[0].name);\nconst bType = bSymbol ? typeChecker.getTypeOfSymbolAtLocation(bSymbol, bSymbol.valueDeclaration!) : undefined; // Type of 'b'\n\nconst dSymbol = typeChecker.getSymbolAtLocation(varStatements[2].declarationList.declarations[0].name);\nconst dType = dSymbol ? typeChecker.getTypeOfSymbolAtLocation(dSymbol, dSymbol.valueDeclaration!) : undefined; // Type of 'd'\n\nif (aType && bType && dType) {\n  // 3. Use ts-simple-type to check assignability and convert types\n  console.log(`Is '${typeToString(toSimpleType(aType, typeChecker))}' assignable to 'number'? ${isAssignableToType(aType, myNumberType, typeChecker)}`); // true\n  console.log(`Is '${typeToString(toSimpleType(bType, typeChecker))}' assignable to 'number'? ${isAssignableToType(bType, myNumberType, typeChecker)}`); // false\n  console.log(`Is '${typeToString(toSimpleType(dType, typeChecker))}' assignable to 'number'? ${isAssignableToType(dType, myNumberType, typeChecker)}`); // true\n\n  // Demonstrate working with SimpleType directly\n  const customStringType: SimpleType = { kind: \"STRING\" };\n  const customNumberType: SimpleType = { kind: \"NUMBER\" };\n  console.log(`Is '${typeToString(toSimpleType(bType, typeChecker))}' assignable to custom STRING type? ${isAssignableToType(bType, customStringType, typeChecker)}`); // true\n  console.log(`Is '${typeToString(toSimpleType(aType, typeChecker))}' assignable to custom NUMBER type? ${isAssignableToType(aType, customNumberType, typeChecker)}`); // true\n}","lang":"typescript","description":"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."},"warnings":[{"fix":"Update calls from `toTypeString(...)` to `typeToString(...)`.","message":"`toTypeString` function was renamed to `typeToString`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Migrate to `typeToString` for string representation of types.","message":"`simpleTypeToString` is no longer exported.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Update property access from `.spread` to `.rest` on `SimpleTypeFunctionParameter` objects.","message":"The `spread` property on `SimpleTypeFunctionParameter` was renamed to `rest`.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Update property access from `.hasRestElement` to `.rest` on `SimpleTypeTuple` objects.","message":"The `hasRestElement` property on `SimpleTypeTuple` was renamed to `rest`.","severity":"breaking","affected_versions":">=1.0.0"},{"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`).","message":"`SimpleTypeKind` and `SimpleTypeModifierKind` were converted from enums to string literal unions.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Access class members via the `.members` property instead of `.methods` or `.properties`.","message":"The `methods` and `properties` properties on `SimpleTypeClass` were renamed to `members`.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"Attempting to use a type function (e.g., `isAssignableToType`) without providing a valid `ts.Type` or `SimpleType` object, or an incorrect `SimpleType` structure.","error":"TypeError: Cannot read properties of undefined (reading 'kind') or Argument of type 'undefined' is not assignable to parameter of type 'SimpleTypeKind'."},{"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.","cause":"Functions like `isAssignableToType` and `toSimpleType` are called with a native `ts.Type` object but without providing a `ts.TypeChecker` instance.","error":"Error: Parameter 'typeChecker' is required when giving a Typescript Type to the function."},{"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`.","cause":"The `typescript` package is not imported or installed when attempting to use its API (e.g., `ts.createProgram`, `ts.ScriptTarget`).","error":"TS2304: Cannot find name 'ts'."}],"ecosystem":"npm"}