https://github.com/ENvironmentSet/ts-transfromer-typerep
Bring type level information to value level.
https://github.com/ENvironmentSet/ts-transfromer-typerep
type-level-programming typescript typescript-transformer
Last synced: 5 months ago
JSON representation
Bring type level information to value level.
- Host: GitHub
- URL: https://github.com/ENvironmentSet/ts-transfromer-typerep
- Owner: ENvironmentSet
- Created: 2021-01-15T09:13:51.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-08-07T07:52:31.000Z (9 months ago)
- Last Synced: 2024-10-30T09:28:03.633Z (6 months ago)
- Topics: type-level-programming, typescript, typescript-transformer
- Language: TypeScript
- Homepage: https://stackblitz.com/edit/webpack-webpack-js-org-7mxehx?file=src%2Fmain.ts
- Size: 49.8 KB
- Stars: 19
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - ts-transfromer-typerep
README
# ts-transformer-typerep
A typescript custom transformer which enables a programmer to pull down type-level(compile time) information into value level(runtime).
```typescript
import { typeRep, TypeKind } from 'ts-transformer-typerep';function keys(): string[] {
const type = typeRep();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
```shell script
npm i -D ts-transformer-typerep
```[Check here](https://github.com/madou/typescript-transformer-handbook#consuming-transformers) 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.| Type | Type Representation |
|------|---------------------|
| `any` | `AnyRep`(`{ kind: TypeKind.Any }`) |
| `number` and its subtypes | `NumberRep`(`{ kind: TypeKind.Number, literal: number }`) |
| `boolean` and its subtypes | `BooleanRep`(`{ kind: TypeKind.Boolean, literal: boolean }`) |
| `string` and its subtypes | `StringRep`(`{ kind: TypeKind.String, literal: string }`) |
| `symbol` | `SymbolRep`(`{ kind: TypeKind.Symbol }`) |
| `bigint` and its subtypes | `BigIntRep`(`{ kind: TypeKind.BigInt, literal: bigint }`) |
| `null` | `NullRep`(`{ kind: TypeKind.null }`) |
| `undefined` | `UndefinedRep`(`{ kind: TypeKind.Undefined }`) |
| `object` | `NonPrimitiveRep`((`{ kind: TypeKind.NonPrimitive }`) |
| `unknown` | `UnknownRep`(`{ kind: TypeKind.Unknown }`) |
| `never` | `NeverRep`(`{ kind: TypeKind.Never }`) |
| `void` | `VoidRep`(`{ kind: TypeKind.Void }`) |
| Any type constructed with Union operator but not `never` | `UnionRep`(`{ kind: TypeKind.Union, parts: TypeRep[] }`) |
| Any type constructed with Intersection operator | `IntersectionRep`(`{ 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`.```typescript
const type = typeRep();if (type.kind === TypeKind.Number) console.log('It\'s a number type!');
else console.log('It\'s not a number type :(');
```| Type | TypeKind |
|------|----------|
| `any` | `Any` |
| `number` and its subtypes | `Number` |
| `boolean` and its subtypes | `Boolean` |
| `string` and its subtypes | `String` |
| `symbol` | `Symbol` |
| `bigint` and its subtypes | `BigInt` |
| `null` | `Null` |
| `undefined` | `Undefined` |
| Any object type excluding subtypes of `Object` | `NonPrimitive` |
| `unknown` | `Unknown` |
| `never` | `Never` |
| `void` | `Void` |
| Any type constructed with Union operator but not `never` | `Union` |
| Any type constructed with Intersection operator | `Intersection` |
| `Object` | `Object` |### Function `typeRep(): TypeRep`
`typeRep` is a function to pull type level information into value level. It returns a type representation of given type.
```typescript
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| Types | Current 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 | ✅ |