diff --git a/src/index.ts b/src/index.ts index 8958e37..2d2a5e3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -297,34 +297,64 @@ const inverted = Symbol('inverted') type Inverted = {[inverted]: T} const expectNull = Symbol('expectNull') -type ExpectNull = {[expectNull]: T; result: ExtendsExcludingAnyOrNever} +type ExpectNull = { + [expectNull]: T + result: ExtendsExcludingAnyOrNever +} const expectUndefined = Symbol('expectUndefined') -type ExpectUndefined = {[expectUndefined]: T; result: ExtendsExcludingAnyOrNever} +type ExpectUndefined = { + [expectUndefined]: T + result: ExtendsExcludingAnyOrNever +} const expectNumber = Symbol('expectNumber') -type ExpectNumber = {[expectNumber]: T; result: ExtendsExcludingAnyOrNever} +type ExpectNumber = { + [expectNumber]: T + result: ExtendsExcludingAnyOrNever +} const expectString = Symbol('expectString') -type ExpectString = {[expectString]: T; result: ExtendsExcludingAnyOrNever} +type ExpectString = { + [expectString]: T + result: ExtendsExcludingAnyOrNever +} const expectBoolean = Symbol('expectBoolean') -type ExpectBoolean = {[expectBoolean]: T; result: ExtendsExcludingAnyOrNever} +type ExpectBoolean = { + [expectBoolean]: T + result: ExtendsExcludingAnyOrNever +} const expectVoid = Symbol('expectVoid') -type ExpectVoid = {[expectVoid]: T; result: ExtendsExcludingAnyOrNever} +type ExpectVoid = { + [expectVoid]: T + result: ExtendsExcludingAnyOrNever +} const expectFunction = Symbol('expectFunction') -type ExpectFunction = {[expectFunction]: T; result: ExtendsExcludingAnyOrNever any>} +type ExpectFunction = { + [expectFunction]: T + result: ExtendsExcludingAnyOrNever any> +} const expectObject = Symbol('expectObject') -type ExpectObject = {[expectObject]: T; result: ExtendsExcludingAnyOrNever} +type ExpectObject = { + [expectObject]: T + result: ExtendsExcludingAnyOrNever +} const expectArray = Symbol('expectArray') -type ExpectArray = {[expectArray]: T; result: ExtendsExcludingAnyOrNever} +type ExpectArray = { + [expectArray]: T + result: ExtendsExcludingAnyOrNever +} const expectSymbol = Symbol('expectSymbol') -type ExpectSymbol = {[expectSymbol]: T; result: ExtendsExcludingAnyOrNever} +type ExpectSymbol = { + [expectSymbol]: T + result: ExtendsExcludingAnyOrNever +} const expectAny = Symbol('expectAny') type ExpectAny = {[expectAny]: T; result: IsAny} @@ -336,7 +366,10 @@ const expectNever = Symbol('expectNever') type ExpectNever = {[expectNever]: T; result: IsNever} const expectNullable = Symbol('expectNullable') -type ExpectNullable = {[expectNullable]: T; result: Not>>} +type ExpectNullable = { + [expectNullable]: T + result: Not>> +} /** * Represents a scolder function that checks if the result of an expecter diff --git a/test/types.test.ts b/test/types.test.ts index 3aac08d..79b963d 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -624,19 +624,27 @@ test('Distinguish between different types that are OR`d together', () => { // @ts-expect-error expectTypeOf<{foo: number} | {bar: string}>().not.toEqualTypeOf<{foo: number} | {bar: string}>() - expectTypeOf<{foo: number} | {bar: string}>().not.toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} | {bar: string}>().not.toEqualTypeOf<{ + foo: number + }>() // @ts-expect-error - expectTypeOf<{foo: number} | {bar: string}>().toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} | {bar: string}>().toEqualTypeOf<{ + foo: number + }>() }) test('Distinguish between identical types that are OR`d together', () => { expectTypeOf<{foo: number} | {foo: number}>().toEqualTypeOf<{foo: number} | {foo: number}>() // Note: The `| T` in `Equal` in index.ts makes this work. - expectTypeOf<{foo: number} | {foo: number}>().toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} | {foo: number}>().toEqualTypeOf<{ + foo: number + }>() // @ts-expect-error expectTypeOf<{foo: number} | {foo: number}>().not.toEqualTypeOf<{foo: number} | {foo: number}>() // @ts-expect-error - expectTypeOf<{foo: number} | {foo: number}>().not.toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} | {foo: number}>().not.toEqualTypeOf<{ + foo: number + }>() }) test('Distinguish between different types that are AND`d together', () => { @@ -653,9 +661,15 @@ test('Distinguish between different types that are AND`d together', () => { test('Works arounds tsc bug not handling intersected types for this form of equivalence', () => { // @ts-expect-error This is the bug. - expectTypeOf<{foo: number} & {bar: string}>().toEqualTypeOf<{foo: number; bar: string}>() + expectTypeOf<{foo: number} & {bar: string}>().toEqualTypeOf<{ + foo: number + bar: string + }>() // This should \@ts-expect-error but does not. - expectTypeOf<{foo: number} & {bar: string}>().not.toEqualTypeOf<{foo: number; bar: string}>() + expectTypeOf<{foo: number} & {bar: string}>().not.toEqualTypeOf<{ + foo: number + bar: string + }>() const one: {foo: number} & {bar: string} = {foo: 1, bar: 'a'} const two: {foo: number; bar: string} = {foo: 1, bar: 'a'} @@ -665,10 +679,16 @@ test('Works arounds tsc bug not handling intersected types for this form of equi expectTypeOf(one).not.toEqualTypeOf(two) // The workaround is the new optional .branded modifier. - expectTypeOf<{foo: number} & {bar: string}>().branded.toEqualTypeOf<{foo: number; bar: string}>() + expectTypeOf<{foo: number} & {bar: string}>().branded.toEqualTypeOf<{ + foo: number + bar: string + }>() expectTypeOf(one).branded.toEqualTypeOf() // @ts-expect-error - expectTypeOf<{foo: number} & {bar: string}>().branded.not.toEqualTypeOf<{foo: number; bar: string}>() + expectTypeOf<{foo: number} & {bar: string}>().branded.not.toEqualTypeOf<{ + foo: number + bar: string + }>() // @ts-expect-error expectTypeOf(one).branded.not.toEqualTypeOf(two) }) @@ -676,13 +696,19 @@ test('Works arounds tsc bug not handling intersected types for this form of equi test('Distinguish between identical types that are AND`d together', () => { expectTypeOf<{foo: number} & {foo: number}>().toEqualTypeOf<{foo: number} & {foo: number}>() // Note: The `& T` in `Equal` in index.ts makes this work. - expectTypeOf<{foo: number} & {foo: number}>().toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} & {foo: number}>().toEqualTypeOf<{ + foo: number + }>() // @ts-expect-error expectTypeOf<{foo: number} & {foo: number}>().not.toEqualTypeOf<{foo: number} & {foo: number}>() // @ts-expect-error - expectTypeOf<{foo: number} & {foo: number}>().not.toEqualTypeOf<{foo: number}>() + expectTypeOf<{foo: number} & {foo: number}>().not.toEqualTypeOf<{ + foo: number + }>() - expectTypeOf<{a: {b: 1} & {c: 1}}>().branded.toEqualTypeOf<{a: {b: 1; c: 1}}>() + expectTypeOf<{a: {b: 1} & {c: 1}}>().branded.toEqualTypeOf<{ + a: {b: 1; c: 1} + }>() expectTypeOf<() => () => () => {a: 1} & {b: 1}>().not.toEqualTypeOf<() => () => () => {a: 1; c: 1}>() expectTypeOf<{foo: number} & {foo: number}>().toEqualTypeOf<{foo: number} & {foo: number}>() @@ -696,9 +722,13 @@ test('limitations', () => { expectTypeOf () => () => void, () => () => () => string>>().toEqualTypeOf() // @ts-expect-error toEqualTypeOf relies on TypeScript's internal `toBeIdentical` function which falls down with intersection types, but is otherwise accurate and performant: https://github.com/microsoft/TypeScript/issues/55188#issuecomment-1656328122 - expectTypeOf<{a: {b: 1} & {c: 1}}>().toEqualTypeOf<{a: {b: 1; c: 1}}>() + expectTypeOf<{a: {b: 1} & {c: 1}}>().toEqualTypeOf<{ + a: {b: 1; c: 1} + }>() // use `.branded` to get around this, at the cost of performance. - expectTypeOf<{a: {b: 1} & {c: 1}}>().branded.toEqualTypeOf<{a: {b: 1; c: 1}}>() + expectTypeOf<{a: {b: 1} & {c: 1}}>().branded.toEqualTypeOf<{ + a: {b: 1; c: 1} + }>() }) test('PrintType', () => { @@ -729,7 +759,9 @@ test('Issue #53: `.omit()` should work similarly to `Omit`', () => { code: number } - expectTypeOf>().toEqualTypeOf<{state: 'loading' | 'failed'}>() + expectTypeOf>().toEqualTypeOf<{ + state: 'loading' | 'failed' + }>() expectTypeOf().omit<'code'>().toEqualTypeOf<{state: 'loading' | 'failed'}>() })