Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Exact): fix support for Date in union #902

Merged
merged 2 commits into from
Jul 2, 2024
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
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://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});
}