Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/changes/expect-type/main_pr-231.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "feat(expect-type): add `guards` & `asserts` getters (#231) - @GerkinDev",
"type": "minor",
"packageName": "expect-type"
}
],
"packageName": "expect-type",
"email": "GerkinDev@users.noreply.github.com"
}
14 changes: 14 additions & 0 deletions packages/expect-type/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ const twoArgFunc = (a: number, b: string) => ({a, b})
expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()
```

You can also check type guards & type assertions:

```typescript
const assertNumber = (v: any): asserts v is number => {
if (typeof v !== 'number') {
throw new TypeError('Nope !')
}
}
expectTypeOf(assertNumber).asserts.toBeNumber()

const isString = (v: any): v is string => typeof v === 'string'
expectTypeOf(isString).guards.toBeString()
```

Assert on constructor parameters:

```typescript
Expand Down
12 changes: 12 additions & 0 deletions packages/expect-type/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ test('More examples of ways to work with functions - parameters using `.paramete
expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()
})

test('You can also check type guards & type assertions', () => {
const assertNumber = (v: any): asserts v is number => {
if (typeof v !== 'number') {
throw new TypeError('Nope !')
}
}
expectTypeOf(assertNumber).asserts.toBeNumber()

const isString = (v: any): v is string => typeof v === 'string'
expectTypeOf(isString).guards.toBeString()
})

test('Assert on constructor parameters', () => {
expectTypeOf(Date).toBeConstructibleWith('1970')
expectTypeOf(Date).toBeConstructibleWith(0)
Expand Down
9 changes: 9 additions & 0 deletions packages/expect-type/src/__tests__/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ test('constructor params', () => {
expectTypeOf<a.ConstructorParams<typeof Date>>().toEqualTypeOf<[] | [string | number | Date]>()
})

test('guarded & asserted types', () => {
expectTypeOf<(v: any) => v is string>().guards.toBeString()
expectTypeOf<(v: any) => asserts v is number>().asserts.toBeNumber()
// @ts-expect-error
expectTypeOf<(v: any) => boolean>().guards.toBeAny()
// @ts-expect-error
expectTypeOf<(v: any) => boolean>().asserts.toBeAny()
})

test('parity with IsExact from conditional-type-checks', () => {
// lifted from https://github.com/dsherret/conditional-type-checks/blob/01215056e8b97a28c5b0311b42ed48c70c8723fe/tests.ts#L18-L63

Expand Down
11 changes: 11 additions & 0 deletions packages/expect-type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ export interface ExpectTypeOf<Actual, B extends boolean> {
returns: Actual extends (...args: any[]) => infer R ? ExpectTypeOf<R, B> : never
resolves: Actual extends PromiseLike<infer R> ? ExpectTypeOf<R, B> : never
items: Actual extends ArrayLike<infer R> ? ExpectTypeOf<R, B> : never
guards: Actual extends (v: any, ...args: any[]) => v is infer T ? ExpectTypeOf<T, B> : never
asserts: Actual extends (v: any, ...args: any[]) => asserts v is infer T
? // Guard methods `(v: any) => asserts v is T` does not actually defines a return type. Thus, any function taking 1 argument matches the signature before.
// In case the inferred assertion type `R` could not be determined (so, `unknown`), consider the function as a non-guard, and return a `never` type.
// See https://github.com/microsoft/TypeScript/issues/34636
unknown extends T
? never
: ExpectTypeOf<T, B>
: never
not: ExpectTypeOf<Actual, Not<B>>
}
const fn: any = () => true
Expand Down Expand Up @@ -153,6 +162,8 @@ export const expectTypeOf: _ExpectTypeOf = <Actual>(actual?: Actual): ExpectType
'items',
'constructorParameters',
'instance',
'guards',
'asserts',
] as const
type Keys = keyof ExpectTypeOf<any, any>

Expand Down