Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stonecypher/is_a_ts
Typescript implementation of generic type guard, inherited from work by Ran Lottem
https://github.com/stonecypher/is_a_ts
Last synced: about 1 month ago
JSON representation
Typescript implementation of generic type guard, inherited from work by Ran Lottem
- Host: GitHub
- URL: https://github.com/stonecypher/is_a_ts
- Owner: StoneCypher
- License: mit
- Created: 2021-07-24T17:38:37.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-30T15:04:18.000Z (over 3 years ago)
- Last Synced: 2024-11-25T04:48:40.548Z (about 2 months ago)
- Language: TypeScript
- Size: 280 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# is_a_ts
Typescript implementation of
[generic type guard, by Ran Lottem](https://dev.to/krumpet/generic-type-guard-in-typescript-258l)[![Node.js CI](https://github.com/StoneCypher/is_a_ts/actions/workflows/node.js.yml/badge.svg)](https://github.com/StoneCypher/is_a_ts/actions/workflows/node.js.yml) [![Coverage Status](https://coveralls.io/repos/github/StoneCypher/is_a_ts/badge.svg?branch=main)](https://coveralls.io/github/StoneCypher/is_a_ts?branch=main)
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/StoneCypher/is_a_ts/blob/master/LICENSEs)
[![NPM Version](https://img.shields.io/npm/v/is_a_ts.svg?style=flat)]()
[![Issues](https://img.shields.io/github/issues-raw/StoneCypher/is_a_ts.svg?maxAge=25000)](https://github.com/StoneCypher/is_a_ts/issues)
[![GitHub contributors](https://img.shields.io/github/contributors/StoneCypher/is_a_ts.svg?style=flat)]()
## What is this?
The mechanism that most Typescript developers use to distinguish types,
`instanceof`, doesn't work at runtime - only compiletime. Making a runtime
version [is a complicated affair](https://dev.to/krumpet/generic-type-guard-in-typescript-258l).This library exists to centralize and test an function called `is_a` (aka
`typeGuard`) to solve this problem.
## ... What is this?
Consider implementing `.from` on a custom container. Don't worry about the long
signature; it's just an example.To implement `.from`, your container has an internal typed `Array`, and so in
implementing `.from`, you'd want to iterate over the input, to make sure all of
the contents fit storage. Initially you might think "well, `source` is typed,
should be fine."```typescript
class SomeContainer {
_storage: T[];
static from(source: Iterable | ArrayLike): T[] {
const inst = new SomeContainer
source.forEach(item => {
if (item instanceof T) { // HA!
_storage.push(T);
}
});}
}
```See the place where The Joker is laughing at you? That's because ***instanceof
is a compile time thing***, and what you're doing requires runtime type
inference.Turns out there isn't a particularly good way to do this in TS. But I found one
on [some developer's blog](https://dev.to/krumpet/generic-type-guard-in-typescript-258l),
so here, we've productionalized it, and automated the test set.It's now relatively simple.
```typescript
import { is_a } from 'is_a_ts';class SomeContainer {
_storage: T[];
static from(source: Iterable | ArrayLike): T[] {
const inst = new SomeContainer
source.forEach(item => {
if (is_a(item, T)) { // Okay then
_storage.push(T);
}
});}
}
```Nice and simple, the way code ought to be.