https://github.com/ksxnodemodules/static-type-assert
Asserting type with help of TypeScript compiler
https://github.com/ksxnodemodules/static-type-assert
assertions types typescipt
Last synced: 4 months ago
JSON representation
Asserting type with help of TypeScript compiler
- Host: GitHub
- URL: https://github.com/ksxnodemodules/static-type-assert
- Owner: ksxnodemodules
- License: mit
- Created: 2018-09-08T18:37:41.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-05-13T01:03:03.000Z (over 1 year ago)
- Last Synced: 2024-11-29T04:35:05.500Z (about 1 year ago)
- Topics: assertions, types, typescipt
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/static-type-assert
- Size: 117 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- awesome-list - static-type-assert
README
# Static Type Assert
Asserting type with help of TypeScript compiler
## Requirements
* Node.js ≥ 8.9.0
* TypeScript ≥ 3.0.0
## Usage
### 1. Create TypeScript files
Example: `pass.ts`
```typescript
import assert from 'static-type-assert'
// Comma-separated parameters
assert('hello', 'world')
assert(123, 456, 789)
// Curry function calls
assert<'abc' | 'def' | 123 | 456>('abc')(123)('def')(456)
// Combine
assert('a', 0)('c', 'd')(1, 2)
```
Example: `fail.ts`
```typescript
import assert from 'static-type-assert'
assert('abc') // Expected Error: Argument of type '"abc"' is not assignable to type 'number'
```
### 2. Use TypeScript compiler to check types
```sh
tsc --noEmit
```
Example: `pass.ts`
```sh
tsc --noEmit pass.ts # Expects to pass (no errors)
```
Example: `fail.ts`
```sh
tsc --noEmit fail.ts # Expects errors
```
## License
[MIT](https://git.io/fA2Ff) © [Hoàng Văn Khải](https://ksxgithub.github.io)