ts-simple-type
Relationship type checker functions for Typescript types.
Last updated 19 days ago by runebm .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ts-simple-type 
SYNC missed versions from official npm registry.

ts-simple-type

Downloads per month NPM Version Contributors MIT License

What is this?

Right now the type checker for Typescript API doesn't expose methods for checking assignability and building types. See issue #9879 and #29432 on the Typescript github repository.

To fill in the gap while this issue is being discussed this library aims to provide the most essential helper functions for working with types in Typescript.

Furthermore, this library can help you construct types (called SimpleType) which can be serialized and easy analyzed.

This library has more than 35000 tests comparing results to actual Typescript diagnostics (see test-types.ts).

Installation

npm install ts-simple-type

How to use

The API is very simple. For example if you want to check if Typescript type typeB is assignable to typeA, you can use the following function.

import { isAssignableToType } from "ts-simple-type";

const isAssignable = isAssignableToType(typeA, typeB, typeChecker);

SimpleType

To make it easier to work with typescript types this library works by (behind the curtain) converting them to the interface SimpleType. Most functions in this library work with both SimpleType and the known and loved Typescript-provided Type interface. This means that you can easily create a complex type yourself and compare it to a native Typescript type. It also means that you can use this library to serialize types and even compare them in the browser.

The SimpleType interface can be used to construct your own types for typechecking.

import { SimpleType, typeToString, isAssignableToType, isAssignableToValue } from "ts-simple-type";

const colors: SimpleType = {
  kind: "UNION",
  types: [
    { kind: "STRING_LITERAL", value: "RED" },
    { kind: "STRING_LITERAL", value: "GREEN" },
    { kind: "STRING_LITERAL", value: "BLUE" }
  ]
};

typeToString(colors)
> "RED" | "GREEN" | "BLUE"

isAssignableToType(colors, { kind: "STRING_LITERAL", value: "YELLOW" })
> false;

isAssignableToValue(colors, "BLUE")
> true;

isAssignableToValue(colors, "PINK")
> false;

More examples

const typeA = checker.getTypeAtLocation(nodeA);
const typeB = checker.getTypeAtLocation(nodeB);

/*
  For this example, let's say:
  - typeA is number
  - typeB is string[]
*/

// typeToString
typeToString(typeA)
> "number"

typeToString(typeB)
> "string[]"


// isAssignableToType
isAssignableToType(typeA, typeB, checker)
> false

isAssignableToType(typeA, { kind: "NUMBER" }, checker)
> true

isAssignableToType(typeB, { kind: "ARRAY", type: {kind: "STRING"}}, checker)
> true

isAssignableToType(
  { kind: "STRING" },
  { kind: "STRING_LITERAL", value: "hello"})
> true


// isAssignableToPrimitiveType
isAssignableToPrimitiveType(typeA, checker)
> true

isAssignableToPrimitiveType(typeB, checker)
> false

isAssignableToPrimitiveType({ kind: "ARRAY", type: {kind: "STRING"} })
> false


// isAssignableToSimpleTypeKind
isAssignableToSimpleTypeKind(typeA, "NUMBER", checker)
> true

isAssignableToSimpleTypeKind(typeB, "BOOLEAN", checker)
> false

isAssignableToSimpleTypeKind(typeB, ["STRING", "UNDEFINED"], checker)
> true


// isAssignableToValue
isAssignableToValue(typeA, 123, checker)
> true

isAssignableToValue(typeA, "hello", checker)
> false

isAssignableToValue(typeB, true, checker)
> false


// toSimpleType
toSimpleType(typeA, {checker})
> { kind: "NUMBER" }

toSimpleType(typeB, {checker})
> { kind: "ARRAY", type: { kind: "NUMBER" } }

API Documentation

For functions that take either a native Typescript Type or a SimpleType the TypeChecker is only required if a Typescript Type has been given to the function.

isAssignableToType

isAssignableToType(typeA: Type | SimpleType, typeB: Type | SimpleType, checker?: TypeChecker): boolean

Returns true if typeB is assignable to typeA.

isAssignableToPrimitiveType

isAssignableToPrimitiveType(type: Type | SimpleType, checker?: TypeChecker): boolean

Returns true if type is assignable to a primitive type like string, number, boolean, bigint, null or undefined.

isAssignableToSimpleTypeKind

isAssignableToSimpleTypeKind(type: Type | SimpleType, kind: SimpleTypeKind | SimpleTypeKind[], checker?: TypeChecker, options?: Options): boolean

Returns true if type is assignable to a SimpleTypeKind.

  • options.matchAny (boolean): Can be used to allow the "any" type to match everything.

isAssignableToValue

isAssignableToValue(type: SimpleType | Type, value: any, checker?: TypeChecker): boolean

Returns true if the type of the value is assignable to type.

typeToString

typeToString(type: SimpleType): string

Returns a string representation of the simple type. The string representation matches the one that Typescript generates.

toSimpleType

toSimpleType(type: Type | Node, checker: TypeChecker): SimpleType

Returns a SimpleType that represents a native Typescript Type.

Current Tags

  • 1.0.7                                ...           latest (19 days ago)
  • 1.0.0-next.2                                ...           next (a month ago)

41 Versions

  • 1.0.7                                ...           19 days ago
  • 1.0.6                                ...           21 days ago
  • 1.0.5                                ...           22 days ago
  • 1.0.4                                ...           22 days ago
  • 1.0.3                                ...           22 days ago
  • 1.0.2                                ...           24 days ago
  • 1.0.1                                ...           24 days ago
  • 1.0.0                                ...           a month ago
  • 1.0.0-next.2                                ...           a month ago
  • 1.0.0-next.1                                ...           2 months ago
  • 0.3.7                                ...           9 months ago
  • 0.3.6                                ...           a year ago
  • 0.3.5                                ...           a year ago
  • 0.3.4                                ...           a year ago
  • 0.3.3                                ...           a year ago
  • 0.3.2                                ...           a year ago
  • 0.3.1                                ...           a year ago
  • 0.3.0                                ...           a year ago
  • 0.2.28                                ...           a year ago
  • 0.2.27                                ...           a year ago
  • 0.2.26                                ...           a year ago
  • 0.2.25                                ...           a year ago
  • 0.2.24                                ...           a year ago
  • 0.2.23                                ...           a year ago
  • 0.2.22                                ...           a year ago
  • 0.2.21                                ...           a year ago
  • 0.2.20                                ...           a year ago
  • 0.2.19                                ...           a year ago
  • 0.2.18                                ...           a year ago
  • 0.2.17                                ...           2 years ago
  • 0.2.16                                ...           2 years ago
  • 0.2.15                                ...           2 years ago
  • 0.2.14                                ...           2 years ago
  • 0.2.12                                ...           2 years ago
  • 0.2.11                                ...           2 years ago
  • 0.2.10                                ...           2 years ago
  • 0.2.9                                ...           2 years ago
  • 0.2.8                                ...           2 years ago
  • 0.2.7                                ...           2 years ago
  • 0.2.6                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 1
Last Week 50
Last Month 474
Dependencies (0)
None
Dev Dependencies (14)

Copyright 2014 - 2017 © taobao.org |