Skip to content
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

.map fn #98

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ const f = (a: number) => [a, a]
expectTypeOf(f).toBeCallableWith('foo')
```

Use `.map` to transform types:

This can be useful for generic functions or complex types which you can't access via `.toBeCallableWith`, `.toHaveProperty` etc. The callback function isn't called at runtime, which can make this a useful way to get complex inferred types without worrying about running code.

```typescript
const capitalize = <S extends string>(input: S) =>
(input.slice(0, 1).toUpperCase() + input.slice(1)) as Capitalize<S>

expectTypeOf(capitalize)
.map(fn => fn('hello world'))
.toEqualTypeOf<'Hello world'>()
```

You can also check type guards & type assertions:

```typescript
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ export interface BaseExpectTypeOf<Actual, Options extends {positive: boolean}> {
*/
toBeNullable: Scolder<ExpectNullable<Actual>, Options>

/**
* Transform that type of the value via a callback.
*
* @param fn A callback that transforms the input value. Note that this function is not actually called - it's only used for type inference.
* @returns A new type which can be used for further assertions.
*/
map: <T>(fn: (value: Actual) => T) => ExpectTypeOf<T, Options>

/**
* Checks whether a function is callable with the given parameters.
*
Expand Down Expand Up @@ -911,6 +919,7 @@ export const expectTypeOf: _ExpectTypeOf = <Actual>(
toMatchTypeOf: fn,
toEqualTypeOf: fn,
toBeConstructibleWith: fn,
map: expectTypeOf,
toBeCallableWith: expectTypeOf,
extract: expectTypeOf,
exclude: expectTypeOf,
Expand Down
13 changes: 13 additions & 0 deletions test/usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ test("You can't use `.toBeCallableWith` with `.not` - you need to use ts-expect-
expectTypeOf(f).toBeCallableWith('foo')
})

/**
* This can be useful for generic functions or complex types which you can't access via `.toBeCallableWith`, `.toHaveProperty` etc.
* The callback function isn't called at runtime, which can make this a useful way to get complex inferred types without worrying about running code.
*/
test('Use `.map` to transform types', () => {
const capitalize = <S extends string>(input: S) =>
(input.slice(0, 1).toUpperCase() + input.slice(1)) as Capitalize<S>

expectTypeOf(capitalize)
.map(fn => fn('hello world'))
.toEqualTypeOf<'Hello world'>()
})

test('You can also check type guards & type assertions', () => {
const assertNumber = (v: any): asserts v is number => {
if (typeof v !== 'number') {
Expand Down