Skip to content

Commit

Permalink
fix(Exact): fix support for Date in union
Browse files Browse the repository at this point in the history
  • Loading branch information
zorji committed Jun 30, 2024
1 parent 476024d commit 57b1f48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions source/exact.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {ArrayElement, ObjectValue} from './internal';
import type {Opaque, TagContainer} from './opaque';
import type {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 @@ -58,6 +59,5 @@ export type Exact<ParameterType, 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;
: 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://github.com/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 57b1f48

Please sign in to comment.