-
-
Notifications
You must be signed in to change notification settings - Fork 110
Add asserts annotations
#194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+213
−18
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dfe9f01
Add `asserts` annotations to main function
mmkal bd8bfda
Add doc comment
mmkal 505bbc5
Simplify casts and add HKT todo
mmkal ee05735
Make type tests more future-proof
mmkal 8ce4859
Update readme.md
sindresorhus 52ea64e
Update match-shape.ts
sindresorhus ea4c252
Update types.ts
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,146 @@ | ||
| import test from 'ava'; | ||
| import {ExpectTypeOf, expectTypeOf} from 'expect-type'; | ||
| import {TypedArray} from 'type-fest'; | ||
| import ow, {BasePredicate} from '../source'; | ||
|
|
||
| test('type-level tests', t => { | ||
| t.is(typeof typeTests, 'function'); | ||
| }); | ||
|
|
||
| // These tests will fail at compile-time, not runtime. | ||
| // The function isn't actually called, it's just a way of declaring scoped type-level tests | ||
| // that doesn't make the compiler angry about unused variables. | ||
| function typeTests(value: unknown) { | ||
| return [ | ||
| () => { | ||
| expectTypeOf(value).toBeUnknown(); | ||
|
|
||
| ow(value, ow.string); | ||
|
|
||
| expectTypeOf(value).toBeString(); | ||
| expectTypeOf(value).not.toBeNever(); | ||
|
|
||
| ow(value, ow.boolean); | ||
|
|
||
| expectTypeOf(value).toBeNever(); // Can't be a string and a boolean! | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, 'my-label', ow.number); | ||
|
|
||
| expectTypeOf(value).toBeNumber(); | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, ow.string.maxLength(7)); | ||
|
|
||
| expectTypeOf(value).toBeString(); | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, ow.optional.string); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<string | undefined>(); | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, ow.iterable); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<Iterable<unknown>>(); | ||
|
|
||
| ow(value, ow.array); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<unknown[]>(); | ||
|
|
||
| ow(value, ow.array.ofType(ow.string)); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<string[]>(); | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, ow.array.ofType(ow.any(ow.string, ow.number, ow.boolean, ow.nullOrUndefined))); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<Array<string | number | boolean | null | undefined>>(); | ||
| }, | ||
|
|
||
| () => { | ||
| ow(value, ow.object); | ||
|
|
||
| expectTypeOf(value).toBeObject(); | ||
|
|
||
| ow(value, ow.object.partialShape({ | ||
| foo: ow.string | ||
| })); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<{ | ||
| foo: string; | ||
| }>(); | ||
|
|
||
| ow(value, ow.object.exactShape({ | ||
| foo: ow.string, | ||
| bar: ow.object.exactShape({ | ||
| baz: ow.number | ||
| }) | ||
| })); | ||
|
|
||
| expectTypeOf(value).toEqualTypeOf<{ | ||
| foo: string; | ||
| bar: {baz: number}; | ||
| }>(); | ||
| }, | ||
|
|
||
| () => { | ||
| // To make sure all validators are mapped to the correct type, create a `Tests` type which requires that | ||
| // every property of `ow` has its type-mapping explicitly tested. If more properties are added this will | ||
| // fail until a type assertion is added below. | ||
|
|
||
| type AssertionProps = Exclude<keyof typeof ow, 'any' | 'isValid' | 'create' | 'optional'>; | ||
|
|
||
| type Tests = { | ||
| [K in AssertionProps]: | ||
| typeof ow[K] extends BasePredicate<infer T> | ||
| ? (type: ExpectTypeOf<T, true>) => void | ||
| : never | ||
| }; | ||
|
|
||
| const tests: Tests = { | ||
| array: expect => expect.toBeArray(), | ||
| arrayBuffer: expect => expect.toEqualTypeOf<ArrayBuffer>(), | ||
| boolean: expect => expect.toBeBoolean(), | ||
| buffer: expect => expect.toEqualTypeOf<Buffer>(), | ||
| dataView: expect => expect.toEqualTypeOf<DataView>(), | ||
| date: expect => expect.toEqualTypeOf<Date>(), | ||
| error: expect => expect.toEqualTypeOf<Error>(), | ||
| float32Array: expect => expect.toEqualTypeOf<Float32Array>(), | ||
| float64Array: expect => expect.toEqualTypeOf<Float64Array>(), | ||
| function: expect => expect.toEqualTypeOf<Function>(), | ||
| int16Array: expect => expect.toEqualTypeOf<Int16Array>(), | ||
| int32Array: expect => expect.toEqualTypeOf<Int32Array>(), | ||
| int8Array: expect => expect.toEqualTypeOf<Int8Array>(), | ||
| iterable: expect => expect.toEqualTypeOf<Iterable<unknown>>(), | ||
| map: expect => expect.toEqualTypeOf<Map<unknown, unknown>>(), | ||
| nan: expect => expect.toEqualTypeOf(Number.NaN), | ||
| null: expect => expect.toEqualTypeOf<null>(), | ||
| nullOrUndefined: expect => expect.toEqualTypeOf<null | undefined>(), | ||
| number: expect => expect.toBeNumber(), | ||
| object: expect => expect.toBeObject(), | ||
| promise: expect => expect.toEqualTypeOf<Promise<unknown>>(), | ||
| regExp: expect => expect.toEqualTypeOf<RegExp>(), | ||
| set: expect => expect.toEqualTypeOf<Set<any>>(), | ||
| sharedArrayBuffer: expect => expect.toEqualTypeOf<SharedArrayBuffer>(), | ||
| string: expect => expect.toBeString(), | ||
| symbol: expect => expect.toEqualTypeOf<symbol>(), | ||
| typedArray: expect => expect.toEqualTypeOf<TypedArray>(), | ||
| uint16Array: expect => expect.toEqualTypeOf<Uint16Array>(), | ||
| uint32Array: expect => expect.toEqualTypeOf<Uint32Array>(), | ||
| uint8Array: expect => expect.toEqualTypeOf<Uint8Array>(), | ||
| uint8ClampedArray: expect => expect.toEqualTypeOf<Uint8ClampedArray>(), | ||
| undefined: expect => expect.toEqualTypeOf<undefined>(), | ||
| weakMap: expect => expect.toEqualTypeOf<WeakMap<object, unknown>>(), | ||
| weakSet: expect => expect.toEqualTypeOf<WeakSet<object>>() | ||
| }; | ||
|
|
||
| return tests; | ||
| } | ||
| ]; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.