Skip to content

Commit

Permalink
v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nshen committed Mar 26, 2021
1 parent 1b222f0 commit 904d092
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "narrowing",
"version": "1.1.0",
"version": "1.1.1",
"description": "TypeScript tiny narrowing helpers you better use.",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
32 changes: 17 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ if (isXXX(a)) {
- isInstance
- isArray

## Demo

```typescript
import {
isString,
Expand Down Expand Up @@ -101,32 +99,36 @@ if (!isNil(b)) {
}
```

### has()
### Type predicates: `has()`

```typescript
type Fish = { swim: () => {} };
type Bird = { fly: () => {} };
type Cat = { run: () => {}; meow: () => {} };
type Dog = { run: () => {} };

function getPet(): any {
return {} as any;
}

//---------------

let pet: Fish | Bird = getPet(); // Fish | Bird
let pet = {} as any;

// type predicates
const isBird = has<Bird>('fly');
const isFish = has('swim');
const isDogOrCat = has<Dog | Cat>('run');
const isCat = has<Cat>('run', 'meow');

if (isBird(pet)) {
pet.fly();
pet.fly(); // Bird
}
if (isFish(pet)) {
pet.swim();

if (isDogOrCat(pet)) {
pet.run(); // Dog | Cat
}

if (isCat(pet)) {
pet.meow(); // Cat
}
```

## Version

- 1.1.0
- add `has()`
- 1.1.1
- `has()` accept multiple params

0 comments on commit 904d092

Please sign in to comment.