-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hand-written type-guard support with ts-auto-guard:custom
- Loading branch information
Showing
3 changed files
with
101 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { testProcessProject } from '../generate' | ||
|
||
testProcessProject( | ||
'imports and uses custom type guard', | ||
{ | ||
'test.ts': ` | ||
/** @see {isFoo} ts-auto-guard:custom */ | ||
export type Foo = string & { brand: true }; | ||
export function isFoo(x: unknown): x is string { | ||
return typeof x === "string"; | ||
} | ||
/** @see {isBar} ts-auto-guard:type-guard */ | ||
export type Bar = { | ||
foo: Foo, | ||
str: string | ||
}`, | ||
}, | ||
{ | ||
'test.ts': null, | ||
'test.guard.ts': ` | ||
import { isFoo, Bar } from "./test"; | ||
export function isBar(obj: unknown): obj is Bar { | ||
const typedObj = obj as Bar | ||
return ( | ||
(typedObj !== null && | ||
typeof typedObj === "object" || | ||
typeof typedObj === "function")&& | ||
isFoo(typedObj["foo"]) as boolean && | ||
typeof typedObj["str"] === "string" | ||
) | ||
}`, | ||
} | ||
) |