-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
assert
, truthy
and falsy
typeguards
- Loading branch information
Showing
3 changed files
with
61 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,15 @@ import test from '../../entrypoints/main.mjs'; | |
type Expected = {foo: 'bar'}; | ||
const expected: Expected = {foo: 'bar'}; | ||
|
||
test('assert', t => { | ||
const actual = expected as Expected | undefined; | ||
if (t.truthy(actual)) { | ||
expectType<Expected>(actual); | ||
} else { | ||
expectType<undefined>(actual); | ||
} | ||
}); | ||
|
||
test('deepEqual', t => { | ||
const actual: unknown = {}; | ||
if (t.deepEqual(actual, expected)) { | ||
|
@@ -33,9 +42,28 @@ test('false', t => { | |
} | ||
}); | ||
|
||
test('falsy', t => { | ||
type Actual = Expected | undefined | null | false | 0 | '' | 0n; | ||
Check failure on line 46 in test-types/module/assertions-as-type-guards.ts
|
||
const actual = undefined as Actual; | ||
if (t.falsy(actual)) { | ||
expectType<Exclude<Actual, Expected>>(actual); | ||
} else { | ||
expectType<Expected>(actual); | ||
} | ||
}); | ||
|
||
test('true', t => { | ||
const actual: unknown = false; | ||
if (t.true(actual)) { | ||
expectType<true>(actual); | ||
} | ||
}); | ||
|
||
test('truthy', t => { | ||
const actual = expected as Expected | undefined; | ||
if (t.truthy(actual)) { | ||
expectType<Expected>(actual); | ||
} else { | ||
expectType<undefined>(actual); | ||
} | ||
}); |
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