ts-auto-guard
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -463,12 +463,5 @@ "use strict"; | ||
var propertyName = property.name; | ||
// can't start with a number (\d) and then only consists of letters, numbers and the underscore (\w) | ||
var validPropertyNameRegex = /^(?!\d)\w+$/; | ||
var isIdentifier = validPropertyNameRegex.test(propertyName); | ||
var strippedName = propertyName.replace(/"/g, ''); | ||
var varName = isIdentifier | ||
? objName + "." + propertyName | ||
: objName + "[\"" + strippedName + "\"]"; | ||
var propertyPath = isIdentifier | ||
? path + "." + propertyName | ||
: path + "[\"" + strippedName + "\"]"; | ||
var varName = objName + "[\"" + strippedName + "\"]"; | ||
var propertyPath = path + "[\"" + strippedName + "\"]"; | ||
var expectedType = property.type.getText(); | ||
@@ -552,3 +545,5 @@ var conditions = typeConditions(varName, property.type, addDependency, project, propertyPath, arrayDepth, true, records, outFile, options); | ||
var defaultArgumentName = lowerFirst(typeName); | ||
var conditions = typeConditions('obj', typeDeclaration.getType(), addDependency, project, '${argumentName}', // tslint:disable-line:no-invalid-template-strings | ||
var signatureObjName = 'obj'; | ||
var innerObjName = 'typedObj'; | ||
var conditions = typeConditions(innerObjName, typeDeclaration.getType(), addDependency, project, '${argumentName}', // tslint:disable-line:no-invalid-template-strings | ||
0, false, records, outFile, options); | ||
@@ -558,11 +553,8 @@ var secondArgument = debug | ||
: ''; | ||
var signature = "export function " + functionName + "(obj: any" + secondArgument + "): obj is " + typeName + " {\n"; | ||
var signature = "export function " + functionName + "(" + signatureObjName + ": unknown" + secondArgument + "): " + signatureObjName + " is " + typeName + " {\n"; | ||
var shortCircuit = shortCircuitCondition | ||
? "if (" + shortCircuitCondition + ") return true\n" | ||
: ''; | ||
return [ | ||
signature, | ||
shortCircuit, | ||
"return (\n" + (conditions || true) + "\n)\n}\n", | ||
].join(''); | ||
var functionBody = "const " + innerObjName + " = " + signatureObjName + " as " + typeName + "\nreturn (\n" + (conditions || true) + "\n)\n}\n"; | ||
return [signature, shortCircuit, functionBody].join(''); | ||
} | ||
@@ -569,0 +561,0 @@ function createAddDependency(dependencies) { |
{ | ||
"name": "ts-auto-guard", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Generate type guard functions from TypeScript interfaces", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rhys-vdw/ts-auto-guard", |
@@ -40,9 +40,11 @@ # ts-auto-guard | ||
export function isPerson(obj: any): obj is Person { | ||
export function isPerson(obj: unknown): obj is Person { | ||
const typedObj = obj as Person | ||
return ( | ||
typeof obj === 'object' && | ||
typeof obj.name === 'string' && | ||
(typeof obj.age === 'undefined' || typeof obj.age === 'number') && | ||
Array.isArray(obj.children) && | ||
obj.children.every(e => isPerson(e)) | ||
typeof typedObj === 'object' && | ||
typeof typedObj['name'] === 'string' && | ||
(typeof typedObj['age'] === 'undefined' || | ||
typeof typedObj['age'] === 'number') && | ||
Array.isArray(typedObj['children']) && | ||
typedObj['children'].every(e => isPerson(e)) | ||
) | ||
@@ -81,3 +83,4 @@ } | ||
/** @see {isPerson} ts-auto-guard:type-guard */ | ||
export interface Person { // !do not forget to export - only exported types are processed | ||
export interface Person { | ||
// !do not forget to export - only exported types are processed | ||
name: string | ||
@@ -92,3 +95,5 @@ age?: number | ||
### Process all types | ||
Use `--export-all` parameter to process all exported types: | ||
``` | ||
@@ -127,8 +132,9 @@ $ ts-auto-guard --export-all 'src/domain/*.ts' | ||
export function isPerson(obj: any): obj is Person { | ||
export function isPerson(obj: unknown): obj is Person { | ||
if (process.env.NODE_ENV === 'production') { | ||
return true | ||
} | ||
const typedObj = obj as Person | ||
return ( | ||
typeof obj === 'object' && | ||
typeof typedObj === 'object' && | ||
// ...normal conditions | ||
@@ -152,2 +158,3 @@ ) | ||
## Add Import to Source File | ||
ts-auto-guard supports an `ìmport-guards` flag. This flag will add an import statement at the top and a named export at the bottom of the source files for the generated type guards. The `ìmport-guards` flag also optionally accepts a custom name for the import alias, if none is passed then `TypeGuards` is used as a default. | ||
@@ -157,3 +164,2 @@ | ||
``` | ||
@@ -164,2 +170,3 @@ $ ts-auto-guard --import-guards="Guards" | ||
Will result in the following being added to your source code. | ||
```ts | ||
@@ -166,0 +173,0 @@ // my-project/Person.ts |
@@ -140,7 +140,8 @@ import test from 'tape' | ||
export function isEmpty(obj: any): obj is Empty { | ||
export function isEmpty(obj: unknown): obj is Empty { | ||
const typedObj = obj as Empty | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") | ||
) | ||
@@ -164,5 +165,6 @@ }`, | ||
export function isBool(obj: any): obj is Bool { | ||
export function isBool(obj: unknown): obj is Bool { | ||
const typedObj = obj as Bool | ||
return ( | ||
typeof obj === "boolean" | ||
typeof typedObj === "boolean" | ||
) | ||
@@ -185,5 +187,6 @@ }`, | ||
export function isBool(obj: any): obj is Bool { | ||
export function isBool(obj: unknown): obj is Bool { | ||
const typedObj = obj as Bool | ||
return ( | ||
typeof obj === "boolean" | ||
typeof typedObj === "boolean" | ||
) | ||
@@ -209,9 +212,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" && | ||
typeof typedObj["bar"] === "string" | ||
) | ||
@@ -265,22 +269,24 @@ }`, | ||
export function isFoo(obj: any, argumentName: string = "foo"): obj is Foo { | ||
export function isFoo(obj: unknown, argumentName: string = "foo"): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
evaluate(typeof obj.foo === "number", \`\${argumentName}.foo\`, "number", obj.foo) && | ||
evaluate(isBar(obj.bar) as boolean, \`\${argumentName}.bar\`, "import(\\"/foo/bar/test\\").Bar", obj.bar) && | ||
evaluate(Array.isArray(obj.bars) && | ||
obj.bars.every((e: any) => | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
evaluate(typeof typedObj["foo"] === "number", \`\${argumentName}["foo"]\`, "number", typedObj["foo"]) && | ||
evaluate(isBar(typedObj["bar"]) as boolean, \`\${argumentName}["bar"]\`, "import(\\"/foo/bar/test\\").Bar", typedObj["bar"]) && | ||
evaluate(Array.isArray(typedObj["bars"]) && | ||
typedObj["bars"].every((e: any) => | ||
isBar(e) as boolean | ||
), \`\${argumentName}.bars\`, "import(\\"/foo/bar/test\\").Bar[]", obj.bars) | ||
), \`\${argumentName}["bars"]\`, "import(\\"/foo/bar/test\\").Bar[]", typedObj["bars"]) | ||
) | ||
} | ||
export function isBar(obj: any, argumentName: string = "bar"): obj is Bar { | ||
export function isBar(obj: unknown, argumentName: string = "bar"): obj is Bar { | ||
const typedObj = obj as Bar | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
evaluate(typeof obj.bar === "number", \`\${argumentName}.bar\`, "number", obj.bar) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
evaluate(typeof typedObj["bar"] === "number", \`\${argumentName}["bar"]\`, "number", typedObj["bar"]) | ||
) | ||
@@ -312,9 +318,10 @@ } | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" && | ||
typeof typedObj["bar"] === "string" | ||
) | ||
@@ -347,9 +354,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" && | ||
typeof typedObj["bar"] === "string" | ||
) | ||
@@ -395,8 +403,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj["${propertyName}"] === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["${propertyName}"] === "number" | ||
) | ||
@@ -421,8 +430,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj["${propertyName}"] === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["${propertyName}"] === "number" | ||
) | ||
@@ -449,9 +459,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj["1"] === "number" && | ||
typeof obj["2"] === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["1"] === "number" && | ||
typeof typedObj["2"] === "string" | ||
) | ||
@@ -477,9 +488,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj["1"] === "number" && | ||
typeof obj["2"] === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["1"] === "number" && | ||
typeof typedObj["2"] === "string" | ||
) | ||
@@ -503,8 +515,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj[""] === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj[""] === "number" | ||
) | ||
@@ -529,8 +542,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj[""] === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj[""] === "number" | ||
) | ||
@@ -558,9 +572,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
typeof obj.bar === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" && | ||
typeof typedObj["bar"] === "string" | ||
) | ||
@@ -587,13 +602,14 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
( typeof obj.foo === "undefined" || | ||
typeof obj.foo === "number" ) && | ||
( typeof obj.bar === "undefined" || | ||
typeof obj.bar === "number" ) && | ||
( typeof obj.baz === "undefined" || | ||
typeof obj.baz === "number" ) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
( typeof typedObj["foo"] === "undefined" || | ||
typeof typedObj["foo"] === "number" ) && | ||
( typeof typedObj["bar"] === "undefined" || | ||
typeof typedObj["bar"] === "number" ) && | ||
( typeof typedObj["baz"] === "undefined" || | ||
typeof typedObj["baz"] === "number" ) | ||
) | ||
@@ -622,11 +638,12 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(obj.foo !== null && | ||
typeof obj.foo === "object" || | ||
typeof obj.foo === "function") && | ||
typeof obj.foo.bar === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
(typedObj["foo"] !== null && | ||
typeof typedObj["foo"] === "object" || | ||
typeof typedObj["foo"] === "function") && | ||
typeof typedObj["foo"]["bar"] === "number" | ||
) | ||
@@ -656,17 +673,19 @@ }`, | ||
export function isBar(obj: any): obj is Bar { | ||
export function isBar(obj: unknown): obj is Bar { | ||
const typedObj = obj as Bar | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "number" | ||
) | ||
} | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
isBar(obj.foo) as boolean | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
isBar(typedObj["foo"]) as boolean | ||
) | ||
@@ -695,9 +714,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "number" && | ||
typeof obj.foo === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "number" && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -727,15 +747,17 @@ }`, | ||
export function isBar(obj: any): obj is Bar { | ||
export function isBar(obj: unknown): obj is Bar { | ||
const typedObj = obj as Bar | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "number" | ||
) | ||
} | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
isBar(obj) as boolean && | ||
typeof obj.foo === "number" | ||
isBar(typedObj) as boolean && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -764,9 +786,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "number" && | ||
typeof obj.foo === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "number" && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -796,15 +819,17 @@ }`, | ||
export function isBar(obj: any): obj is Bar { | ||
export function isBar(obj: unknown): obj is Bar { | ||
const typedObj = obj as Bar | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "number" | ||
) | ||
} | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
isBar(obj) as boolean && | ||
typeof obj.foo === "number" | ||
isBar(typedObj) as boolean && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -829,8 +854,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -858,8 +884,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -884,9 +911,10 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
if (DEBUG) return true | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" | ||
) | ||
@@ -943,25 +971,28 @@ }`, | ||
export function isPropertyValueType(obj: any): obj is PropertyValueType { | ||
export function isPropertyValueType(obj: unknown): obj is PropertyValueType { | ||
const typedObj = obj as PropertyValueType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.value === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["value"] === "string" | ||
) | ||
} | ||
export function isPropertyName(obj: any): obj is PropertyName { | ||
export function isPropertyName(obj: unknown): obj is PropertyName { | ||
const typedObj = obj as PropertyName | ||
return ( | ||
(obj === "name" || | ||
obj === "value") | ||
(typedObj === "name" || | ||
typedObj === "value") | ||
) | ||
} | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
isPropertyValueType(obj.name) as boolean && | ||
isPropertyValueType(obj.value) as boolean | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
isPropertyValueType(typedObj["name"]) as boolean && | ||
isPropertyValueType(typedObj["value"]) as boolean | ||
) | ||
@@ -992,7 +1023,8 @@ } | ||
export function isBranch1(obj: any): obj is Branch1 { | ||
export function isBranch1(obj: unknown): obj is Branch1 { | ||
const typedObj = obj as Branch1 | ||
return ( | ||
(typeof obj === "string" || | ||
Array.isArray(obj) && | ||
obj.every((e: any) => | ||
(typeof typedObj === "string" || | ||
Array.isArray(typedObj) && | ||
typedObj.every((e: any) => | ||
isBranch1(e) as boolean | ||
@@ -1003,10 +1035,11 @@ )) | ||
export function isBranch2(obj: any): obj is Branch2 { | ||
export function isBranch2(obj: unknown): obj is Branch2 { | ||
const typedObj = obj as Branch2 | ||
return ( | ||
(typeof obj === "string" || | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.branches) && | ||
obj.branches.every((e: any) => | ||
(typeof typedObj === "string" || | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Array.isArray(typedObj["branches"]) && | ||
typedObj["branches"].every((e: any) => | ||
isBranch2(e) as boolean | ||
@@ -1017,18 +1050,19 @@ )) | ||
export function isBranch3(obj: any): obj is Branch3 { | ||
export function isBranch3(obj: unknown): obj is Branch3 { | ||
const typedObj = obj as Branch3 | ||
return ( | ||
(typeof obj === "string" || | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.branches) && | ||
obj.branches.every((e: any) => | ||
(typeof typedObj === "string" || | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Array.isArray(typedObj["branches"]) && | ||
typedObj["branches"].every((e: any) => | ||
isBranch3(e) as boolean | ||
) || | ||
Array.isArray(obj) && | ||
obj.every((e: any) => | ||
Array.isArray(typedObj) && | ||
typedObj.every((e: any) => | ||
(e !== null && | ||
typeof e === "object" || | ||
typeof e === "function") && | ||
isBranch3(e.branches) as boolean | ||
isBranch3(e["branches"]) as boolean | ||
)) | ||
@@ -1052,14 +1086,15 @@ ) | ||
export function isX(obj: any): obj is X { | ||
export function isX(obj: unknown): obj is X { | ||
const typedObj = obj as X | ||
return ( | ||
((obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
obj.type === "a" && | ||
typeof obj.value === "number" || | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
obj.type === "b" && | ||
typeof obj.value === "string") | ||
((typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typedObj["type"] === "a" && | ||
typeof typedObj["value"] === "number" || | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typedObj["type"] === "b" && | ||
typeof typedObj["value"] === "string") | ||
) | ||
@@ -1086,7 +1121,8 @@ }`, | ||
export function isTypes(obj: any): obj is Types { | ||
export function isTypes(obj: unknown): obj is Types { | ||
const typedObj = obj as Types | ||
return ( | ||
(obj === Types.TheGood || | ||
obj === Types.TheBad || | ||
obj === Types.TheTypeSafe) | ||
(typedObj === Types.TheGood || | ||
typedObj === Types.TheBad || | ||
typedObj === Types.TheTypeSafe) | ||
) | ||
@@ -1116,24 +1152,26 @@ }`, | ||
export function isTypes(obj: any): obj is Types { | ||
export function isTypes(obj: unknown): obj is Types { | ||
const typedObj = obj as Types | ||
return ( | ||
(obj === Types.TheGood || | ||
obj === Types.TheBad || | ||
obj === Types.TheTypeSafe) | ||
(typedObj === Types.TheGood || | ||
typedObj === Types.TheBad || | ||
typedObj === Types.TheTypeSafe) | ||
) | ||
} | ||
export function isTestItem(obj: any): obj is TestItem { | ||
export function isTestItem(obj: unknown): obj is TestItem { | ||
const typedObj = obj as TestItem | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(obj.room !== null && | ||
typeof obj.room === "object" || | ||
typeof obj.room === "function") && | ||
(typeof obj.room["1"] === "undefined" || | ||
typeof obj.room["1"] === "string") && | ||
(typeof obj.room["2"] === "undefined" || | ||
typeof obj.room["2"] === "string") && | ||
(typeof obj.room["3"] === "undefined" || | ||
typeof obj.room["3"] === "string") | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
(typedObj["room"] !== null && | ||
typeof typedObj["room"] === "object" || | ||
typeof typedObj["room"] === "function") && | ||
(typeof typedObj["room"]["1"] === "undefined" || | ||
typeof typedObj["room"]["1"] === "string") && | ||
(typeof typedObj["room"]["2"] === "undefined" || | ||
typeof typedObj["room"]["2"] === "string") && | ||
(typeof typedObj["room"]["3"] === "undefined" || | ||
typeof typedObj["room"]["3"] === "string") | ||
) | ||
@@ -1159,8 +1197,9 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.value) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Array.isArray(typedObj["value"]) | ||
) | ||
@@ -1188,14 +1227,15 @@ }`, | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.value) && | ||
obj.value.every((e: any) => | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Array.isArray(typedObj["value"]) && | ||
typedObj["value"].every((e: any) => | ||
(e !== null && | ||
typeof e === "object" || | ||
typeof e === "function") && | ||
Array.isArray(e.value) && | ||
e.value.every((e: any) => | ||
Array.isArray(e["value"]) && | ||
e["value"].every((e: any) => | ||
typeof e === "number" | ||
@@ -1225,8 +1265,9 @@ ) | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([_key, value]) => (typeof value === "string")) | ||
@@ -1236,8 +1277,9 @@ ) | ||
export function isSecondaryTestType(obj: any): obj is SecondaryTestType { | ||
export function isSecondaryTestType(obj: unknown): obj is SecondaryTestType { | ||
const typedObj = obj as SecondaryTestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([_key, value]) => (typeof value === "string")) | ||
@@ -1272,7 +1314,8 @@ ) | ||
export function isEmpty(obj: any): obj is Empty { | ||
export function isEmpty(obj: unknown): obj is Empty { | ||
const typedObj = obj as Empty | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") | ||
) | ||
@@ -1307,6 +1350,7 @@ }`, | ||
export function isTestTypeList(obj: any): obj is TestTypeList { | ||
export function isTestTypeList(obj: unknown): obj is TestTypeList { | ||
const typedObj = obj as TestTypeList | ||
return ( | ||
Array.isArray(obj) && | ||
obj.every((e: any) => | ||
Array.isArray(typedObj) && | ||
typedObj.every((e: any) => | ||
isTestType(e) as boolean | ||
@@ -1320,9 +1364,10 @@ ) | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(typeof obj.someKey === "string" || | ||
typeof obj.someKey === "number") | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
(typeof typedObj["someKey"] === "string" || | ||
typeof typedObj["someKey"] === "number") | ||
) | ||
@@ -1351,10 +1396,11 @@ } | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(obj.someKey === "some" || | ||
obj.someKey === "key") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
(typedObj["someKey"] === "some" || | ||
typedObj["someKey"] === "key") && | ||
Object.entries<any>(typedObj) | ||
.filter(([key]) => !["someKey"].includes(key)) | ||
@@ -1386,8 +1432,9 @@ .every(([key, value]) => ((value === "string" || | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([key, value]) => ((value === "string" || | ||
@@ -1417,8 +1464,9 @@ value === "dynamic") && | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([key, _value]) => (typeof key === "string")) | ||
@@ -1446,8 +1494,9 @@ ) | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([_key, value]) => (typeof value === "string")) | ||
@@ -1483,8 +1532,9 @@ ) | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Object.entries<any>(obj) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Object.entries<any>(typedObj) | ||
.every(([key, _value]) => (typeof key === "string")) | ||
@@ -1512,7 +1562,8 @@ ) | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") | ||
) | ||
@@ -1547,11 +1598,12 @@ } | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.test === "function" && | ||
typeof obj.test3 === "function" && | ||
typeof obj.test3.test3Arg === "number" && | ||
typeof obj.test2 === "function" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["test"] === "function" && | ||
typeof typedObj["test3"] === "function" && | ||
typeof typedObj["test3"]["test3Arg"] === "number" && | ||
typeof typedObj["test2"] === "function" | ||
) | ||
@@ -1580,6 +1632,7 @@ } | ||
export function isTestType(obj: any): obj is TestType { | ||
export function isTestType(obj: unknown): obj is TestType { | ||
const typedObj = obj as TestType | ||
return ( | ||
typeof obj === "function" && | ||
typeof obj.arg === "number" | ||
typeof typedObj === "function" && | ||
typeof typedObj["arg"] === "number" | ||
) | ||
@@ -1603,12 +1656,13 @@ } | ||
export function isX(obj: any): obj is X { | ||
export function isX(obj: unknown): obj is X { | ||
const typedObj = obj as X | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.foo === "number" && | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.bar === "string" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["foo"] === "number" && | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["bar"] === "string" | ||
) | ||
@@ -1633,9 +1687,10 @@ }`, | ||
export function isA(obj: any): obj is A { | ||
export function isA(obj: unknown): obj is A { | ||
const typedObj = obj as A | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
Array.isArray(obj.b) && | ||
typeof obj.b[0] === "number" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
Array.isArray(typedObj["b"]) && | ||
typeof typedObj["b"][0] === "number" | ||
) | ||
@@ -1657,5 +1712,6 @@ }`, | ||
export function isA(obj: any): obj is A { | ||
export function isA(obj: unknown): obj is A { | ||
const typedObj = obj as A | ||
return ( | ||
Array.isArray(obj) | ||
Array.isArray(typedObj) | ||
) | ||
@@ -1677,3 +1733,4 @@ }`, | ||
export function isA(obj: any): obj is A { | ||
export function isA(obj: unknown): obj is A { | ||
const typedObj = obj as A | ||
return ( | ||
@@ -1697,3 +1754,4 @@ true | ||
export function isA(obj: any): obj is A { | ||
export function isA(obj: unknown): obj is A { | ||
const typedObj = obj as A | ||
return ( | ||
@@ -1723,3 +1781,4 @@ true | ||
export function isAnyOrString(obj: any): obj is AnyOrString { | ||
export function isAnyOrString(obj: unknown): obj is AnyOrString { | ||
const typedObj = obj as AnyOrString | ||
return ( | ||
@@ -1730,3 +1789,4 @@ true | ||
export function isUnknownOrString(obj: any): obj is UnknownOrString { | ||
export function isUnknownOrString(obj: unknown): obj is UnknownOrString { | ||
const typedObj = obj as UnknownOrString | ||
return ( | ||
@@ -1737,3 +1797,4 @@ true | ||
export function isAnyOrUnknownOrString(obj: any): obj is AnyOrUnknownOrString { | ||
export function isAnyOrUnknownOrString(obj: unknown): obj is AnyOrUnknownOrString { | ||
const typedObj = obj as AnyOrUnknownOrString | ||
return ( | ||
@@ -1763,3 +1824,4 @@ true | ||
export function isAnyAndString(obj: any): obj is AnyAndString { | ||
export function isAnyAndString(obj: unknown): obj is AnyAndString { | ||
const typedObj = obj as AnyAndString | ||
return ( | ||
@@ -1770,9 +1832,11 @@ true | ||
export function isUnknownAndString(obj: any): obj is UnknownAndString { | ||
export function isUnknownAndString(obj: unknown): obj is UnknownAndString { | ||
const typedObj = obj as UnknownAndString | ||
return ( | ||
typeof obj === "string" | ||
typeof typedObj === "string" | ||
) | ||
} | ||
export function isAnyAndUnknownAndString(obj: any): obj is AnyAndUnknownAndString { | ||
export function isAnyAndUnknownAndString(obj: unknown): obj is AnyAndUnknownAndString { | ||
const typedObj = obj as AnyAndUnknownAndString | ||
return ( | ||
@@ -1779,0 +1843,0 @@ true |
@@ -73,13 +73,14 @@ import test from 'tape' | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
(obj.target !== null && | ||
typeof obj.target === "object" || | ||
typeof obj.target === "function") && | ||
(typeof obj.target.skipLoadingLibFiles === "undefined" || | ||
obj.target.skipLoadingLibFiles === false || | ||
obj.target.skipLoadingLibFiles === true) | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
(typedObj["target"] !== null && | ||
typeof typedObj["target"] === "object" || | ||
typeof typedObj["target"] === "function") && | ||
(typeof typedObj["target"]["skipLoadingLibFiles"] === "undefined" || | ||
typedObj["target"]["skipLoadingLibFiles"] === false || | ||
typedObj["target"]["skipLoadingLibFiles"] === true) | ||
) | ||
@@ -97,8 +98,9 @@ } | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
typeof obj.target === "function" | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typeof typedObj["target"] === "function" | ||
) | ||
@@ -117,8 +119,9 @@ } | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
obj.target instanceof CompilerOptionsContainer | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typedObj["target"] instanceof CompilerOptionsContainer | ||
) | ||
@@ -139,10 +142,11 @@ } | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
obj.target instanceof CompilerOptionsContainer && | ||
obj.res instanceof TsConfigResolver && | ||
obj.fs instanceof InMemoryFileSystemHost | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typedObj["target"] instanceof CompilerOptionsContainer && | ||
typedObj["res"] instanceof TsConfigResolver && | ||
typedObj["fs"] instanceof InMemoryFileSystemHost | ||
) | ||
@@ -161,8 +165,9 @@ } | ||
export function isFoo(obj: any): obj is Foo { | ||
export function isFoo(obj: unknown): obj is Foo { | ||
const typedObj = obj as Foo | ||
return ( | ||
(obj !== null && | ||
typeof obj === "object" || | ||
typeof obj === "function") && | ||
obj.dir instanceof Directory | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function") && | ||
typedObj["dir"] instanceof Directory | ||
) | ||
@@ -169,0 +174,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
139757
2891
174