Skip to content

Commit 62af668

Browse files
docs(README.md): add example to isNotUndefined
1 parent 7a7e8cf commit 62af668

File tree

2 files changed

+66
-128
lines changed

2 files changed

+66
-128
lines changed

README.md

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -764,57 +764,6 @@ The **return value** is a `boolean` indicating whether or not the `value` is an
764764

765765
```typescript
766766
// Example usage
767-
const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
768-
const SYMBOL_STRING: unique symbol = Symbol(STRING);
769-
770-
/**
771-
* typeof === 'number'
772-
* instanceof Function === false
773-
* instanceof Number === false
774-
* instanceof Object === false
775-
*/
776-
const NUMBER: any = 10304050;
777-
778-
/**
779-
* typeof === 'number'
780-
* instanceof Function === false
781-
* instanceof Number === false
782-
* instanceof Object === false
783-
*/
784-
const NUMBER_INSTANCE: any = Number(NUMBER);
785-
786-
/**
787-
* typeof === 'number'
788-
* instanceof Function === false
789-
* instanceof Number === true
790-
* instanceof Object === true
791-
*/
792-
const NUMBER_NEW_INSTANCE: any = new Number(NUMBER);
793-
794-
/**
795-
* typeof === 'string'
796-
* instanceof Function === false
797-
* instanceof Object === false
798-
* instanceof String === false
799-
*/
800-
const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz';
801-
802-
/**
803-
* typeof === 'string'
804-
* instanceof Function === false
805-
* instanceof Object === false
806-
* instanceof String === false
807-
*/
808-
const STRING_INSTANCE: any = String(STRING);
809-
810-
/**
811-
* typeof === 'string'
812-
* instanceof Function === false
813-
* instanceof Object === true
814-
* instanceof String === true
815-
*/
816-
const STRING_NEW_INSTANCE: any = new String(STRING);
817-
818767
const OBJECT_ONE: ObjectOne = {
819768
'key as string': true,
820769
1030405027: 'key is number',
@@ -827,13 +776,6 @@ const OBJECT_ONE: ObjectOne = {
827776
};
828777

829778
isObject(OBJECT_ONE); // true
830-
isObject(OBJECT_ONE, STRING); // true
831-
isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true
832-
isObject(OBJECT_ONE, 1030405027); // true
833-
isObject(OBJECT_ONE, NUMBER); // true
834-
isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true
835-
isObject(OBJECT_ONE, SYMBOL_NUMBER); // true
836-
isObject(OBJECT_ONE, SYMBOL_STRING); // true
837779

838780
```
839781

@@ -982,7 +924,7 @@ export class Class {
982924
*/
983925
export const CLASS = new Class();
984926

985-
// One of the differences between `in` operator and the `hasOwnProperty()` method is that it doesn't find a getter key
927+
// One of the differences between the `in` operator and the `hasOwnProperty()` method is that it doesn't find a getter key
986928
isObjectKey(CLASS, SYMBOL_NUMBER); // false
987929
isObjectKey(CLASS, SYMBOL_STRING); // false
988930
isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // false
@@ -993,7 +935,7 @@ isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // false
993935

994936
### isObjectKeyIn
995937

996-
![https://img.shields.io/badge/-New-green](https://img.shields.io/badge/-New-green)
938+
![new][new]
997939

998940
Use `isObjectKeyIn()` or `is.objectKeyIn()` to check if **any** `value` is an [`Object`][object] with the `key` of the [`Key`][key] type by using the `in` operator. The function uses operator [`in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) to find the key.
999941

@@ -1123,7 +1065,7 @@ const isString: IsString = (value: any, callback: ResultCallback = resultCallbac
11231065
| Parameter | Type | Description |
11241066
| :-------- | :---------------------------------: | :------------------- |
11251067
| value | `any` | Any `value` to check |
1126-
| callback | [`ResultCallback`][resultcallback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
1068+
| callback | [`ResultCallback`][resultcallback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
11271069

11281070
The **return value** is a `boolean` indicating whether or not the `value` is a `string`.
11291071

@@ -1156,9 +1098,9 @@ const isStringType: IsStringType = (value: any, callback: ResultCallback = resul
11561098
callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string', value);
11571099
```
11581100

1159-
| Parameter | Type | Description |
1160-
| :-------- | :---------------------------------------------------------------------: | :------------------- |
1161-
| value | `any` | Any `value` to check |
1101+
| Parameter | Type | Description |
1102+
| :-------- | :-------------------------------------------------------------: | :------------------- |
1103+
| value | `any` | Any `value` to check |
11621104
| callback | [`ResultCallback`][resultcallback]=[`resultCallback`][callback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
11631105

11641106
The **return value** is a `boolean` indicating whether or not the `value` is a `string`.
@@ -1398,6 +1340,33 @@ const isNotUndefined: IsNotUndefined = (value: unknown, callback: ResultCallback
13981340

13991341
The return value is a `boolean` indicating whether or not the `value` is not `undefined`.
14001342

1343+
```typescript
1344+
// Example usage with the problem
1345+
interface Config {
1346+
a?: string;
1347+
b?: string;
1348+
}
1349+
let config: Config = {
1350+
a: 'x',
1351+
b: 'y'
1352+
};
1353+
1354+
function configFunction(value: string): string {
1355+
return '';
1356+
}
1357+
1358+
// Cause typescript return `boolean` this will generate an type error
1359+
if (is.not.undefined(config.a)) {
1360+
configFunction(config.a);
1361+
}
1362+
1363+
// Cause typescript return `value is boolean` this will not generate an error
1364+
if (!is.undefined(config.a)) {
1365+
configFunction(config.a);
1366+
}
1367+
1368+
```
1369+
14011370
----
14021371

14031372
## Guard

packages/type/README.md

Lines changed: 33 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -764,57 +764,6 @@ The **return value** is a `boolean` indicating whether or not the `value` is an
764764

765765
```typescript
766766
// Example usage
767-
const SYMBOL_NUMBER: unique symbol = Symbol(NUMBER);
768-
const SYMBOL_STRING: unique symbol = Symbol(STRING);
769-
770-
/**
771-
* typeof === 'number'
772-
* instanceof Function === false
773-
* instanceof Number === false
774-
* instanceof Object === false
775-
*/
776-
const NUMBER: any = 10304050;
777-
778-
/**
779-
* typeof === 'number'
780-
* instanceof Function === false
781-
* instanceof Number === false
782-
* instanceof Object === false
783-
*/
784-
const NUMBER_INSTANCE: any = Number(NUMBER);
785-
786-
/**
787-
* typeof === 'number'
788-
* instanceof Function === false
789-
* instanceof Number === true
790-
* instanceof Object === true
791-
*/
792-
const NUMBER_NEW_INSTANCE: any = new Number(NUMBER);
793-
794-
/**
795-
* typeof === 'string'
796-
* instanceof Function === false
797-
* instanceof Object === false
798-
* instanceof String === false
799-
*/
800-
const STRING: any = '!@#$%^&*()abcdefghijklmnoprstuwyz';
801-
802-
/**
803-
* typeof === 'string'
804-
* instanceof Function === false
805-
* instanceof Object === false
806-
* instanceof String === false
807-
*/
808-
const STRING_INSTANCE: any = String(STRING);
809-
810-
/**
811-
* typeof === 'string'
812-
* instanceof Function === false
813-
* instanceof Object === true
814-
* instanceof String === true
815-
*/
816-
const STRING_NEW_INSTANCE: any = new String(STRING);
817-
818767
const OBJECT_ONE: ObjectOne = {
819768
'key as string': true,
820769
1030405027: 'key is number',
@@ -827,13 +776,6 @@ const OBJECT_ONE: ObjectOne = {
827776
};
828777

829778
isObject(OBJECT_ONE); // true
830-
isObject(OBJECT_ONE, STRING); // true
831-
isObject(OBJECT_ONE, STRING_NEW_INSTANCE); // true
832-
isObject(OBJECT_ONE, 1030405027); // true
833-
isObject(OBJECT_ONE, NUMBER); // true
834-
isObject(OBJECT_ONE, NUMBER_NEW_INSTANCE); // true
835-
isObject(OBJECT_ONE, SYMBOL_NUMBER); // true
836-
isObject(OBJECT_ONE, SYMBOL_STRING); // true
837779

838780
```
839781

@@ -982,7 +924,7 @@ export class Class {
982924
*/
983925
export const CLASS = new Class();
984926

985-
// One of the differences between `in` operator and the `hasOwnProperty()` method is that it doesn't find a getter key
927+
// One of the differences between the `in` operator and the `hasOwnProperty()` method is that it doesn't find a getter key
986928
isObjectKey(CLASS, SYMBOL_NUMBER); // false
987929
isObjectKey(CLASS, SYMBOL_STRING); // false
988930
isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // false
@@ -993,7 +935,7 @@ isObjectKey(CLASS, [SYMBOL_NUMBER, SYMBOL_STRING]); // false
993935

994936
### isObjectKeyIn
995937

996-
![https://img.shields.io/badge/-New-green](https://img.shields.io/badge/-New-green)
938+
![new][new]
997939

998940
Use `isObjectKeyIn()` or `is.objectKeyIn()` to check if **any** `value` is an [`Object`][object] with the `key` of the [`Key`][key] type by using the `in` operator. The function uses operator [`in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in) to find the key.
999941

@@ -1123,7 +1065,7 @@ const isString: IsString = (value: any, callback: ResultCallback = resultCallbac
11231065
| Parameter | Type | Description |
11241066
| :-------- | :---------------------------------: | :------------------- |
11251067
| value | `any` | Any `value` to check |
1126-
| callback | [`ResultCallback`][resultcallback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
1068+
| callback | [`ResultCallback`][resultcallback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
11271069

11281070
The **return value** is a `boolean` indicating whether or not the `value` is a `string`.
11291071

@@ -1156,9 +1098,9 @@ const isStringType: IsStringType = (value: any, callback: ResultCallback = resul
11561098
callback(value instanceof Object === false && value instanceof String === false && typeof value === 'string', value);
11571099
```
11581100

1159-
| Parameter | Type | Description |
1160-
| :-------- | :---------------------------------------------------------------------: | :------------------- |
1161-
| value | `any` | Any `value` to check |
1101+
| Parameter | Type | Description |
1102+
| :-------- | :-------------------------------------------------------------: | :------------------- |
1103+
| value | `any` | Any `value` to check |
11621104
| callback | [`ResultCallback`][resultcallback]=[`resultCallback`][callback] | [`ResultCallback`][resultcallback] function to handle result before returns eg. to throw an `Error` |
11631105

11641106
The **return value** is a `boolean` indicating whether or not the `value` is a `string`.
@@ -1398,6 +1340,33 @@ const isNotUndefined: IsNotUndefined = (value: unknown, callback: ResultCallback
13981340

13991341
The return value is a `boolean` indicating whether or not the `value` is not `undefined`.
14001342

1343+
```typescript
1344+
// Example usage with the problem
1345+
interface Config {
1346+
a?: string;
1347+
b?: string;
1348+
}
1349+
let config: Config = {
1350+
a: 'x',
1351+
b: 'y'
1352+
};
1353+
1354+
function configFunction(value: string): string {
1355+
return '';
1356+
}
1357+
1358+
// Cause typescript return `boolean` this will generate an type error
1359+
if (is.not.undefined(config.a)) {
1360+
configFunction(config.a);
1361+
}
1362+
1363+
// Cause typescript return `value is boolean` this will not generate an error
1364+
if (!is.undefined(config.a)) {
1365+
configFunction(config.a);
1366+
}
1367+
1368+
```
1369+
14011370
----
14021371

14031372
## Guard

0 commit comments

Comments
 (0)