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

Improve CLI error messages #16

Merged
merged 62 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
50005ee
toEqualTypeOf generic constraint
mmkal Oct 23, 2022
ce87fc4
also toMatchTypeOf although message is slightly misleading
mmkal Oct 23, 2022
c688e72
Merge branch 'main' into dx-generics
mmkal Oct 23, 2022
e779861
format typescript diagnostics in error test
mmkal Oct 23, 2022
a851221
Merge branch 'main' into dx-generics
mmkal Oct 23, 2022
1a660a9
Update error snapshots
mmkal Oct 23, 2022
a0a1171
Merge branch 'main' into dx-generics
mmkal Oct 23, 2022
aa08372
get rid of ...MISMATCH??
mmkal Oct 23, 2022
14e9ef2
handle never/any edge cases
mmkal Oct 23, 2022
bfc0e0b
Merge branch 'main' into dx-generics
mmkal Oct 23, 2022
da5bafd
Update snapshot
mmkal Oct 23, 2022
7324e5d
Move exhaustive any/unknown/never tetsts out of usage.test.ts
mmkal Oct 24, 2022
1504ad1
rm temp thing
mmkal Oct 24, 2022
368e9f0
update snapshot
mmkal Oct 24, 2022
9ca8f44
rm unused type
mmkal Oct 24, 2022
6677de3
ignore line numbers
mmkal Apr 8, 2023
f28b88f
map leaf types
mmkal Apr 9, 2023
516e0be
Fallback on ...MISMATCH
mmkal Apr 9, 2023
3ae12fc
Rename
mmkal Apr 9, 2023
984bb31
deprecate inferred-from-value expectations
mmkal Apr 9, 2023
f12f366
old-usage.test.ts for deprecated method
mmkal Apr 9, 2023
68e7fcf
beef up literals test
mmkal Apr 10, 2023
cf19226
Merge remote-tracking branch 'origin/main' into dx-generics
mmkal Apr 13, 2023
7a249bc
Merge remote-tracking branch 'origin/main' into dx-generics
mmkal May 29, 2023
dfdaa7c
docs
mmkal May 29, 2023
7771bc1
file rename to improve diff
mmkal Sep 30, 2023
89c13d4
lint
mmkal Sep 30, 2023
2642719
typo
mmkal Sep 30, 2023
244e337
scolder idea but something's up w nullables in test
mmkal Sep 30, 2023
bd8c4a2
make ts-morph respect typescript setup
mmkal Sep 30, 2023
6c41d4e
Get rid of confusing unions
mmkal Oct 1, 2023
0d3ed14
bust a union
mmkal Oct 1, 2023
6f16f1b
refactor into more focused interfaces; document `.branded` limitation
mmkal Oct 1, 2023
777b0ff
update snapshots
mmkal Oct 1, 2023
2b75b95
AValue
mmkal Oct 1, 2023
148fc07
naive rename back to toEqualTypeOf
mmkal Oct 1, 2023
3cf7dcf
help ts choose the right overload
mmkal Oct 1, 2023
e00a538
back to usage.test.ts
mmkal Oct 1, 2023
7f0dd06
missed renames
mmkal Oct 1, 2023
9e99421
restore old tests more
mmkal Oct 1, 2023
15c2cab
more stable error snapshots
mmkal Oct 1, 2023
41ccc60
rm old usage
mmkal Oct 1, 2023
57229b0
external snapshot for the big one
mmkal Oct 1, 2023
0260222
Merge branch 'main' into dx-generics
mmkal Oct 1, 2023
5156e47
restore tests from main
mmkal Oct 1, 2023
55d8a44
mv toHaveProperty to positive-only assertions
mmkal Oct 1, 2023
f6d885f
some docs
mmkal Oct 1, 2023
f6e96d4
fix #32
mmkal Oct 1, 2023
50da767
document https://github.com/mmkal/expect-type/issues/4
mmkal Oct 1, 2023
05d5b68
document #6
mmkal Oct 1, 2023
1e45309
update snapshot
mmkal Oct 1, 2023
f44c1ef
0.17.0-0
mmkal Oct 1, 2023
5891d8f
bump np
mmkal Oct 1, 2023
fa81d73
0.17.0-1
mmkal Oct 1, 2023
37ef8c8
single-use symbols
mmkal Oct 1, 2023
f3e6ec4
more code(!)
mmkal Oct 1, 2023
16cee72
flatter ternaries
mmkal Oct 2, 2023
b0640d1
rm unused
mmkal Oct 2, 2023
3f165d7
to be or not to be
mmkal Oct 2, 2023
68f0a69
document the preference for `<>` over `()`
mmkal Oct 2, 2023
0f664da
0.17.0-2
mmkal Oct 2, 2023
88752b6
tweak error messages docs
mmkal Oct 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ To allow for extra properties, use `.toMatchTypeOf`. This checks that an object
expectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})
```

`.toEqualTypeOf` and `.toMatchTypeOf` both fail on missing properties:

```typescript
// @ts-expect-error
expectTypeOf({a: 1}).toEqualTypeOf({a: 1, b: 1})
// @ts-expect-error
expectTypeOf({a: 1}).toMatchTypeOf({a: 1, b: 1})
```
mmkal marked this conversation as resolved.
Show resolved Hide resolved

Another example of the difference between `.toMatchTypeOf` and `.toEqualTypeOf`, using generics. `.toMatchTypeOf` can be used for "is-a" relationships:

```typescript
Expand Down
35 changes: 31 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export type Or<Types extends boolean[]> = Types[number] extends false ? false :
export type And<Types extends boolean[]> = Types[number] extends true ? true : false
export type Eq<Left extends boolean, Right extends boolean> = Left extends true ? Right : Not<Right>
export type Xor<Types extends [boolean, boolean]> = Not<Eq<Types[0], Types[1]>>
export type Ternary<B extends boolean, ValueIfTrue, ValueIfFalse> = B extends true ? ValueIfTrue : ValueIfFalse
mmkal marked this conversation as resolved.
Show resolved Hide resolved

const secret = Symbol('secret')
type Secret = typeof secret
Expand All @@ -11,6 +12,13 @@ export type IsNever<T> = [T] extends [never] ? true : false
export type IsAny<T> = [T] extends [Secret] ? Not<IsNever<T>> : false
export type IsUnknown<T> = [unknown] extends [T] ? Not<IsAny<T>> : false
export type IsNeverOrAny<T> = Or<[IsNever<T>, IsAny<T>]>
export type BrandSpecial<T> = IsAny<T> extends true
? {special: true; type: 'any'}
: IsUnknown<T> extends true
? {special: true; type: 'unknown'}
: IsNever<T> extends true
? {special: true; type: 'never'}
: never

/**
* Recursively walk a type and replace it with a branded type related to the original. This is useful for
Expand Down Expand Up @@ -94,6 +102,7 @@ export type ConstructorParams<Actual> = Actual extends new (...args: infer P) =>
: never

type MismatchArgs<B extends boolean, C extends boolean> = Eq<B, C> extends true ? [] : [never]
type Mismatch<T> = (BrandSpecial<T> | T) & {[secret]: 'Type should be satisified'}

export interface ExpectTypeOf<Actual, B extends boolean> {
toBeAny: (...MISMATCH: MismatchArgs<IsAny<Actual>, B>) => true
Expand All @@ -111,12 +120,30 @@ export interface ExpectTypeOf<Actual, B extends boolean> {
toBeUndefined: (...MISMATCH: MismatchArgs<Extends<Actual, undefined>, B>) => true
toBeNullable: (...MISMATCH: MismatchArgs<Not<Equal<Actual, NonNullable<Actual>>>, B>) => true
toMatchTypeOf: {
<Expected>(...MISMATCH: MismatchArgs<Extends<Actual, Expected>, B>): true
<Expected>(expected: Expected, ...MISMATCH: MismatchArgs<Extends<Actual, Expected>, B>): true
<Expected extends B extends true ? (Extends<Actual, Expected> extends true ? unknown : Mismatch<Actual>) : unknown>(
...MISMATCH: Or<[IsNeverOrAny<Actual>, IsNeverOrAny<Expected>]> extends true
? MismatchArgs<Extends<Actual, Expected>, B>
: []
): true
<Expected extends B extends true ? (Extends<Actual, Expected> extends true ? unknown : Mismatch<Actual>) : unknown>(
expected: Expected,
...MISMATCH: Or<[IsNeverOrAny<Actual>, IsNeverOrAny<Expected>]> extends true
? MismatchArgs<Extends<Actual, Expected>, B>
: []
): true
}
toEqualTypeOf: {
<Expected>(...MISMATCH: MismatchArgs<Equal<Actual, Expected>, B>): true
<Expected>(expected: Expected, ...MISMATCH: MismatchArgs<Equal<Actual, Expected>, B>): true
<Expected extends B extends true ? (Equal<Actual, Expected> extends true ? unknown : Mismatch<Actual>) : unknown>(
...MISMATCH: Or<[IsNeverOrAny<Actual>, IsNeverOrAny<Expected>]> extends true
? MismatchArgs<Equal<Actual, Expected>, B>
: []
): true
<Expected extends B extends true ? (Equal<Actual, Expected> extends true ? unknown : Mismatch<Actual>) : unknown>(
expected: Expected,
...MISMATCH: Or<[IsNeverOrAny<Actual>, IsNeverOrAny<Expected>]> extends true
? MismatchArgs<Equal<Actual, Expected>, B>
: []
): true
}
toBeCallableWith: B extends true ? (...args: Params<Actual>) => true : never
toBeConstructibleWith: B extends true ? (...args: ConstructorParams<Actual>) => true : never
Expand Down