typescanner
Advanced tools
Comparing version 0.2.0 to 0.2.1
{ | ||
"name": "typescanner", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "A simple library for implementing type guards in TypeScript.", | ||
@@ -10,2 +10,6 @@ "author": "yona3", | ||
}, | ||
"keywords": [ | ||
"typescript", | ||
"type-guard" | ||
], | ||
"bugs": { | ||
@@ -12,0 +16,0 @@ "url": "https://github.com/yona3/typescanner/issues" |
@@ -95,6 +95,9 @@ # typescanner | ||
list(["a", "b", "c"]); // "a" | "b" | "c" | ||
// Pass a constructor as argument | ||
instanceOf(Error) | ||
``` | ||
### scanner | ||
`scanner` is a function for implementing type guard for Objects. It returns a `Condition` of the type defined in Type Aliase by setting a field to the value of each property. | ||
`scanner` is a function to implement type guard for objects. It returns a "type guard function" of the type defined by Type Aliase by setting a field to the value of each property. | ||
```ts | ||
@@ -111,4 +114,9 @@ type Foo = { | ||
i: string | number; | ||
j: number; | ||
}; | ||
// You can extend fields by defining your own type guard functions. | ||
const even = (value: unknown): value is number => | ||
isNumber(value) && value % 2 === 0; | ||
const isFoo = scanner<Foo>({ | ||
@@ -124,2 +132,3 @@ a: string, | ||
i: union<string | number>(string, number), | ||
j: even, // Custom field | ||
}); | ||
@@ -135,2 +144,3 @@ | ||
g: "a", | ||
j: 2, | ||
} as unknown; | ||
@@ -155,5 +165,8 @@ | ||
### primitive | ||
### other | ||
Basic type guard functions with function names beginning with "is". | ||
```ts | ||
// primitive | ||
isString("a") | ||
@@ -174,7 +187,5 @@ | ||
isBigint(BigInt(1)) | ||
``` | ||
### isArray | ||
// isArray | ||
```ts | ||
isArray(["a", "b"], isString) // string[] | ||
@@ -185,15 +196,11 @@ | ||
isArray(["a", null, undefined], isString, isNull, isUndefined) // (string | null | undefined)[] | ||
``` | ||
### isOptional | ||
// isOptional | ||
```ts | ||
isOptional("a", isString) // true | ||
isOptional(undefined, isString) // true | ||
``` | ||
### isList | ||
// isList | ||
```ts | ||
const Lang = { | ||
@@ -207,3 +214,13 @@ ja: "ja", | ||
isList("ja", langList) | ||
isList("ja", langList) // true | ||
// isInstanceOf | ||
try { | ||
... | ||
} catch (error) { | ||
if (isInstanceOf(error, Error)) { | ||
error.message // OK | ||
} | ||
} | ||
``` | ||
@@ -210,0 +227,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36530
227