Skip to content

Commit

Permalink
Exact: Fix support for Date in union (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
zorji authored Jul 2, 2024
1 parent 60c1024 commit d89a709
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
9 changes: 4 additions & 5 deletions source/exact.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {ArrayElement, ObjectValue} from './internal';
import type {Opaque, TagContainer} from './opaque';
import type {IsEqual} from './is-equal';
import type {KeysOfUnion} from './keys-of-union';
import type {JsonObject} from './basic';

/**
Create a type from `ParameterType` and `InputType` and change keys exclusive to `InputType` to `never`.
Expand Down Expand Up @@ -57,7 +57,6 @@ export type Exact<ParameterType, InputType> =
: 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>>>
// Leave tagged types as-is. We could try to make the untagged part Exact, and just leave the tag as-is, but that seems to create instanitation excessively deep errors.
: ParameterType extends TagContainer<unknown> ? ParameterType
: ParameterType extends object ? ExactObject<ParameterType, InputType>
: ParameterType;
// Only apply Exact for pure object types. For types from a class, leave it unchanged to TypeScript to handle.
: ParameterType extends JsonObject ? ExactObject<ParameterType, InputType>
: ParameterType;
17 changes: 17 additions & 0 deletions test-d/exact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,20 @@ import type {Exact, Opaque} from '../index';
},
});
}

// Spec - special test case for Date type + union
// @see https:/sindresorhus/type-fest/issues/896
{
type A = {
a: string;
b?: Date | null;
};

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

function_({a: 'a'});
function_({a: 'a', b: new Date()});
function_({a: 'a', b: new Date() as Date | null});
// @ts-expect-error
function_({a: 'a', b: 1});
}

0 comments on commit d89a709

Please sign in to comment.