Skip to content

Commit

Permalink
add has()
Browse files Browse the repository at this point in the history
  • Loading branch information
nshen committed Mar 26, 2021
1 parent 4cf9156 commit 733664e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 41 deletions.
38 changes: 36 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ if (isXXX(a)) {

## Functions

### typeof narrowing

- isString
- isNumber
- isBigInt
Expand Down Expand Up @@ -95,4 +93,40 @@ if (isInstance(a, TestClass)) {
if (isArray<string>(a)) {
a[0].trim();
}

let b: TestClass | undefined | null;
// b.m(); // Error: Object is possibly 'null' or 'undefined'.ts(2533)
if (!isNil(b)) {
b.m(); // no Error any more
}
```

### has()

```typescript
type Fish = { swim: () => {} };
type Bird = { fly: () => {} };

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

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

let pet: Fish | Bird = getPet(); // Fish | Bird

const isBird = has<Bird>('fly');
const isFish = has('swim');

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

## Version

- 1.1.0
- add `has()`
15 changes: 2 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,8 @@ export function isArray<T>(value: unknown): value is Array<T> {
return Array.isArray(value);
}

// export function as<T>(value: unknown): value is T {
// return true;
// }

// export function is<T>(
// typeFuc: (v: unknown) => boolean,
// value: unknown
// ): value is T {
// return typeFuc(value);
// }

export function is<T>(typeFunc: (v: unknown) => boolean) {
export function has<T>(key: keyof T) {
return function is(b: unknown): b is T {
return typeFunc(b);
return !isNil((b as any)[key]);
};
}
41 changes: 15 additions & 26 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isInstance,
isArray,
isNil,
is
has
} from './index';

let a: unknown;
Expand Down Expand Up @@ -58,40 +58,29 @@ if (isArray<string>(a)) {
}

let b: TestClass | undefined | null;
// b.m(); // Error: Object is possibly 'null' or 'undefined'.ts(2533)
if (!isNil(b)) {
b.m(); // no Error any more
}

b.m(); // Error: Object is possibly 'null' or 'undefined'.ts(2533)
/*
* has()
*/

type Fish = { swim: () => {} };
type Bird = { fly: () => {} };

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

// const isFish = is<Fish>(pet, (pet)=>{
// return !isNil((pet as Fish).swim);
// })

// if(isFish(pet)){

// pet

// }

export function has<T>(key: keyof T) {
return function is(b: unknown): b is T {
return !isNil((b as any)[key]);
};
}

const isBird = has<Bird>('fly');
const isFish = has('swim');

if (has('swim')) {
if (isBird(pet)) {
pet.fly();
}
if (isFish(pet)) {
pet.swim();
}

// const isFish = is<Fish>((p)=>{ return !isNil((p as any).swim)})

// function isFish2(p):p is Fish{
// return !isNil((p as any).swim)
// }

0 comments on commit 733664e

Please sign in to comment.