Skip to content

Commit

Permalink
Move IsEqual to be first in Exact comparison to short-circuit before …
Browse files Browse the repository at this point in the history
…distribution
  • Loading branch information
abrenneke committed Aug 28, 2024
1 parent 0fdc7d2 commit ecbfeed
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
25 changes: 13 additions & 12 deletions source/exact.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ onlyAcceptNameImproved(invalidInput); // Compilation error
@category Utilities
*/
export type Exact<ParameterType, InputType> =
// If the parameter is a primitive, return it as is immediately to avoid it being converted to a complex type
ParameterType extends Primitive ? ParameterType
// If the parameter is an unknown, return it as is immediately to avoid it being converted to a complex type
: IsUnknown<ParameterType> extends true ? unknown
// If the parameter is a Function, return it as is because this type is not capable of handling function, leave it to TypeScript
: ParameterType extends Function ? ParameterType
: IsEqual<ParameterType, InputType> extends true ? ParameterType
// Convert union of array to array of union: A[] & B[] => (A & B)[]
: ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
// In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.
: ParameterType extends readonly unknown[] ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
: ExactObject<ParameterType, InputType>;
// Before distributing, check if the two types are equal and if so, return the parameter type immediately
IsEqual<ParameterType, InputType> extends true ? ParameterType
// If the parameter is a primitive, return it as is immediately to avoid it being converted to a complex type
: ParameterType extends Primitive ? ParameterType
// If the parameter is an unknown, return it as is immediately to avoid it being converted to a complex type
: IsUnknown<ParameterType> extends true ? unknown
// If the parameter is a Function, return it as is because this type is not capable of handling function, leave it to TypeScript
: ParameterType extends Function ? ParameterType
// Convert union of array to array of union: A[] & B[] => (A & B)[]

Check failure on line 64 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 4 tabs but found 6.

Check failure on line 64 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 4 tabs but found 6.

Check failure on line 64 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 4 tabs but found 6.
: ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>

Check failure on line 65 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 5 tabs but found 6.

Check failure on line 65 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 5 tabs but found 6.

Check failure on line 65 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 5 tabs but found 6.
// In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.

Check failure on line 66 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 5 tabs but found 7.

Check failure on line 66 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 5 tabs but found 7.

Check failure on line 66 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 5 tabs but found 7.
: ParameterType extends readonly unknown[] ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>

Check failure on line 67 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 6 tabs but found 7.

Check failure on line 67 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 6 tabs but found 7.

Check failure on line 67 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 6 tabs but found 7.
: ExactObject<ParameterType, InputType>;

Check failure on line 68 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

Expected indentation of 7 tabs but found 8.

Check failure on line 68 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

Expected indentation of 7 tabs but found 8.

Check failure on line 68 in source/exact.d.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

Expected indentation of 7 tabs but found 8.
12 changes: 12 additions & 0 deletions test-d/exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,15 @@ import type {Exact, Opaque} from '../index';
// @ts-expect-error
function_(withExcessSurname);
}

// Spec - recursive type with union
// @see https://github.com/sindresorhus/type-fest/issues/948
{
type A = { a: A };

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

There should be no space after '{'.

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

There should be no space before '}'.

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

There should be no space after '{'.

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

There should be no space before '}'.

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

There should be no space after '{'.

Check failure on line 544 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

There should be no space before '}'.
type B = { b: string };

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

There should be no space after '{'.

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 20

There should be no space before '}'.

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

There should be no space after '{'.

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 18

There should be no space before '}'.

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

There should be no space after '{'.

Check failure on line 545 in test-d/exact.ts

View workflow job for this annotation

GitHub Actions / Node.js 16

There should be no space before '}'.
type Expected = A | B;

const function_ = <T extends Exact<Expected, T>>(arguments_: T) => arguments_;

function_({} as Expected);
}

0 comments on commit ecbfeed

Please sign in to comment.