Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huy-dna/meta-ts
A repo containing facilities for metaprogramming in typescript
https://github.com/huy-dna/meta-ts
metaprogramming typescript
Last synced: 22 days ago
JSON representation
A repo containing facilities for metaprogramming in typescript
- Host: GitHub
- URL: https://github.com/huy-dna/meta-ts
- Owner: Huy-DNA
- License: apache-2.0
- Created: 2024-06-04T17:18:10.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-09-02T11:33:43.000Z (2 months ago)
- Last Synced: 2024-09-15T12:45:36.976Z (about 2 months ago)
- Topics: metaprogramming, typescript
- Language: TypeScript
- Homepage:
- Size: 12.2 MB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# meta-ts
[![npm](https://img.shields.io/npm/v/@huy-dna/meta-ts.svg)](https://www.npmjs.com/package/@huy-dna/meta-ts) [![Download](https://img.shields.io/npm/dm/@huy-dna/meta-ts)](https://www.npmjs.com/package/@huy-dna/meta-ts)
A package containing facilities for metaprogramming in typescript
## Installation
```bash
npm install @huy-dna/meta-ts
```## Usage
The main checker is the two (dummy) functions `checkAll` and `check`:
```typescript
checkAll<[
Predicate,
Predicate,
Predicate,
]>
```and
```typescript
check
```We can think of `check` and `checkAll` as:
* Compile-time functions - do not cause runtime overhead
* Arguments can be passed using `<>`.## Predicates
A predicate is an assertion about types.
A predicate `P` is said to be "true" if and only if `check
` pass the Typescript typechecker.
Currently, these predicates are supported:
### `SameType`
This predicate is true if `T` and `S` are the same type.
Example:
```typescript
check> // passed!
check> // failed!
```### `DoesExtend`
This predicate is true if `T` extends `S`.
Example:
```typescript
check> // failed!
check> // passed!
```### `SameNumericalEnum`
This predicate is true if enum `T` and enum `S` have the same keys with the same numerical value.
Example:
```typescript
enum E1 {
A,
B,
C
}enum E2 {
A = 0,
B,
C = 2,
}check> // passed!
```Note that this only works with number-valued enums!
## Pitfalls
* May not work correctly with `any`.
## Contribution
This package is mainly built to tweak with the Typescript's typechecker.
Don't hesitate to challenge yourself and contribute some "predicate"s to this repo here: https://github.com/Huy-DNA/meta-ts.
## Bug reports
Post an issues on the package's repo!