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/fix-constructor-params_pr-245.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "Fix constructor parameter comparison (#245)",
"type": "minor",
"packageName": "expect-type"
}
],
"packageName": "expect-type",
"email": "mmkal@users.noreply.github.com"
}
28 changes: 28 additions & 0 deletions packages/expect-type/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,34 @@ type E2 = {a: string; b: {c: string}}
expectTypeOf<A2>().toMatchTypeOf<E2>()
expectTypeOf<A2>().not.toEqualTypeOf<E2>()
```

Distinguish between classes with different constructors:

```typescript
class A {
value: number
constructor(a: 1) {
this.value = a
}
}
class B {
value: number
constructor(b: 2) {
this.value = b
}
}

expectTypeOf<typeof A>().not.toEqualTypeOf<typeof B>()

class C {
value: number
constructor(c: 1) {
this.value = c
}
}

expectTypeOf<typeof A>().toEqualTypeOf<typeof C>()
```
<!-- codegen:end -->

### Within test frameworks
Expand Down
26 changes: 26 additions & 0 deletions packages/expect-type/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,29 @@ test('Detect the difference between regular and readonly properties', () => {
expectTypeOf<A2>().toMatchTypeOf<E2>()
expectTypeOf<A2>().not.toEqualTypeOf<E2>()
})

test('Distinguish between classes with different constructors', () => {
class A {
value: number
constructor(a: 1) {
this.value = a
}
}
class B {
value: number
constructor(b: 2) {
this.value = b
}
}

expectTypeOf<typeof A>().not.toEqualTypeOf<typeof B>()

class C {
value: number
constructor(c: 1) {
this.value = c
}
}

expectTypeOf<typeof A>().toEqualTypeOf<typeof C>()
})
16 changes: 13 additions & 3 deletions packages/expect-type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,32 @@ export type IsNeverOrAny<T> = Or<[IsNever<T>, IsAny<T>]>
* - `{ readonly a: string }` vs `{ a: string }`
* - `{ a?: string }` vs `{ a: string | undefined }`
*/
type DeepBrand<T> = IsAny<T> extends true // avoid `any` matching `unknown`
? Secret
export type DeepBrand<T> = Or<[IsNever<T>, IsAny<T>, IsUnknown<T>]> extends true // avoid `any`/`unknown`/`never` matching
? {
type: 'special'
never: IsNever<T>
any: IsAny<T>
unknown: IsUnknown<T>
}
: T extends string | number | boolean | symbol | bigint | null | undefined
? T
? {
type: 'primitive'
value: T
}
: T extends (...args: infer P) => infer R // avoid functions with different params/return values matching
? {
type: 'function'
params: DeepBrand<P>
return: DeepBrand<R>
constructorParams: DeepBrand<ConstructorParams<T>>
}
: {
type: 'object'
properties: {[K in keyof T]: DeepBrand<T[K]>}
readonly: ReadonlyKeys<T>
required: RequiredKeys<T>
optional: OptionalKeys<T>
constructorParams: DeepBrand<ConstructorParams<T>>
}

export type RequiredKeys<T> = Extract<
Expand Down