0.0.0-6 • Published 3 years ago

ts-transformer-typerep v0.0.0-6

Weekly downloads
33
License
-
Repository
github
Last release
3 years ago

ts-transformer-typerep

A typescript custom transformer which enables a programmer to pull down type-level(compile time) information into value level(runtime).

import { typeRep, TypeKind } from 'ts-transformer-typerep';

function keys<T>(): string[] {
  const type = typeRep<T>();

  if (type.kind === TypeKind.Object) return type.properties.map(([key]) => key);
  else return [];
}

keys<{ x: 1, y: 2, z: 3 }>(); // ['x', 'y', 'z']

WARNING⚠️: This transformer is on experiment. It's not recommended to use this in product.

Installation

npm i -D ts-transformer-typerep

Check here to learn how to apply custom transformers.

Reference

Type TypeRep

Type representations are values that represent types. TypeRep is type of all type representation. Since typescript types can be classified in several groups, type representations are classified in groups, too.

TypeType Representation
anyAnyRep({ kind: TypeKind.Any })
number and its subtypesNumberRep({ kind: TypeKind.Number, literal: number })
boolean and its subtypesBooleanRep({ kind: TypeKind.Boolean, literal: boolean })
string and its subtypesStringRep({ kind: TypeKind.String, literal: string })
symbolSymbolRep({ kind: TypeKind.Symbol })
bigint and its subtypesBigIntRep({ kind: TypeKind.BigInt, literal: bigint })
nullNullRep({ kind: TypeKind.null })
undefinedUndefinedRep({ kind: TypeKind.Undefined })
objectNonPrimitiveRep(({ kind: TypeKind.NonPrimitive })
unknownUnknownRep({ kind: TypeKind.Unknown })
neverNeverRep({ kind: TypeKind.Never })
voidVoidRep({ kind: TypeKind.Void })
Any type constructed with Union operator but not neverUnionRep({ kind: TypeKind.Union, parts: TypeRep[] })
Any type constructed with Intersection operatorIntersectionRep({ kind: TypeKind.Intersection, parts: TypeRep[] })

Type TypeKind

TypeKind is type of values for discriminating different type representations. Different type representations are distinguished by its kind field, whose value is value of TypeKind.

const type = typeRep<number>();

if (type.kind === TypeKind.Number) console.log('It\'s a number type!');
else console.log('It\'s not a number type :(');
TypeTypeKind
anyAny
number and its subtypesNumber
boolean and its subtypesBoolean
string and its subtypesString
symbolSymbol
bigint and its subtypesBigInt
nullNull
undefinedUndefined
Any object type excluding subtypes of ObjectNonPrimitive
unknownUnknown
neverNever
voidVoid
Any type constructed with Union operator but not neverUnion
Any type constructed with Intersection operatorIntersection
ObjectObject

Function typeRep<typeToPullDown>(): TypeRep

typeRep is a function to pull type level information into value level. It returns a type representation of given type.

typeRep<10>();
/**
{
  kind: TypeKind.Number,
  literal: 10
}
**/

WARNING⚠️: This function is not fully implemented yet, please refer to following 'Type Support Table' to check which types it supports.

Type Support Table

  • Available(✅): A type is fully supported
  • WIP(🚧): A type is partially supported
  • Todo(📝): A type is planned to be supported
TypesCurrent State
Primitive types
Literal types
never/unknown/any/void
Polymorphic types🚧(Single type variable such as T, A only)
Enums📝
Function types
Union types
Intersection types
Template literal types📝
Object types
0.0.0-6

3 years ago

0.0.0-5

3 years ago

0.0.0-4

3 years ago

0.0.0-3

3 years ago

0.0.0-2

3 years ago

0.0.0-1

3 years ago

0.0.0

3 years ago