Skip to content
Open
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
12 changes: 5 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23705,8 +23705,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Check if source type `S` is related to target type `{ [P in Q]: T }` or `{ [P in Q as R]: T}`.
const keysRemapped = !!target.declaration.nameType;
const templateType = getTemplateTypeFromMappedType(target);
const modifiers = getMappedTypeModifiers(target);
if (!(modifiers & MappedTypeModifiers.ExcludeOptional)) {
const combinedOptionality = getCombinedMappedTypeOptionality(target);
if (combinedOptionality !== -1) {
// If the mapped type has shape `{ [P in Q]: T[P] }`,
// source `S` is related to target if `T` = `S`, i.e. `S` is related to `{ [P in Q]: S[P] }`.
if (
Expand All @@ -23721,7 +23721,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const targetKeys = keysRemapped ? getNameTypeFromMappedType(target)! : getConstraintTypeFromMappedType(target);
// Type of the keys of source type `S`, i.e. `keyof S`.
const sourceKeys = getIndexType(source, IndexFlags.NoIndexSignatures);
const includeOptional = modifiers & MappedTypeModifiers.IncludeOptional;
const includeOptional = combinedOptionality === 1;
const filteredByApplicability = includeOptional ? intersectTypes(targetKeys, sourceKeys) : undefined;
// A source type `S` is related to a target type `{ [P in Q]: T }` if `Q` is related to `keyof S` and `S[Q]` is related to `T`.
// A source type `S` is related to a target type `{ [P in Q as R]: T }` if `R` is related to `keyof S` and `S[R]` is related to `T.
Expand All @@ -23738,10 +23738,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Fastpath: When the template type has the form `Obj[P]` where `P` is the mapped type parameter, directly compare source `S` with `Obj`
// to avoid creating the (potentially very large) number of new intermediate types made by manufacturing `S[P]`.
const nonNullComponent = extractTypesOfKind(templateType, ~TypeFlags.Nullable);
if (!keysRemapped && nonNullComponent.flags & TypeFlags.IndexedAccess && (nonNullComponent as IndexedAccessType).indexType === typeParameter) {
if (result = isRelatedTo(source, (nonNullComponent as IndexedAccessType).objectType, RecursionFlags.Target, reportErrors)) {
return result;
}
if (!keysRemapped && nonNullComponent.flags & TypeFlags.IndexedAccess && (nonNullComponent as IndexedAccessType).indexType === typeParameter && (result = isRelatedTo(source, (nonNullComponent as IndexedAccessType).objectType, RecursionFlags.Target, reportErrors))) {
return result;
}
else {
// We need to compare the type of a property on the source type `S` to the type of the same property on the target type,
Expand Down
98 changes: 98 additions & 0 deletions tests/baselines/reference/mappedTypeRelationships2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
mappedTypeRelationships2.ts(21,38): error TS2344: Type 'ABC' does not satisfy the constraint 'WithNumber<Record<keyof T, any>>'.
mappedTypeRelationships2.ts(67,5): error TS2353: Object literal may only specify known properties, and 'special' does not exist in type 'Lookup<"common">'.
mappedTypeRelationships2.ts(78,9): error TS2322: Type '{ field1: undefined; field2: undefined; special: 32; }' is not assignable to type 'Lookup<K>'.


==== mappedTypeRelationships2.ts (3 errors) ====
// https://github.com/microsoft/TypeScript/issues/62717

type Alias1<T extends object, U extends { [K in keyof T]?: any }> = U;
type Alias2<T extends object, U extends Partial<Record<keyof T, any>>> = U;

type AB = { a: string; b: string };
type B = { b: string };

type Test1 = Alias1<AB, B>; // ok
type Test2 = Alias2<AB, B>; // ok

type Test3<T extends AB> = Alias1<T, B>; // ok
type Test4<T extends AB> = Alias2<T, B>; // ok

type WithNumber<T> = { [K in keyof T]: T[K] | number };
type Alias3<T extends object, U extends WithNumber<Record<keyof T, any>>> = U;

type ABC = { a: string; b: string; c: string };

type Test5 = Alias3<AB, ABC>; // ok
type Test6<T extends AB> = Alias3<T, ABC>; // error
~~~
!!! error TS2344: Type 'ABC' does not satisfy the constraint 'WithNumber<Record<keyof T, any>>'.

type Alias4<T extends object, U extends WithNumber<Partial<Record<keyof T, any>>>> = U;

type Test7 = Alias4<AB, ABC>; // ok
type Test8<T extends AB> = Alias4<T, ABC>; // ok

type Alias5<T extends object, U extends Partial<WithNumber<Record<keyof T, any>>>> = U;

type Test9 = Alias5<AB, ABC>; // ok
type Test10<T extends AB> = Alias5<T, ABC>; // ok

// part of https://github.com/microsoft/TypeScript/issues/62770

interface WithCommon {
field1: number;
field2: string;
}

interface WithSpecial extends WithCommon {
special: number;
}

interface SpecialLookup {
special: WithSpecial;
}

interface CommonLookup extends SpecialLookup {
common: WithCommon;
}

type Lookup<Name extends keyof CommonLookup> = {
[K in keyof CommonLookup[Name]]?: CommonLookup[Name][K];
};
function someName<K extends keyof SpecialLookup>(): void {
// ok
const works1: Lookup<"special"> = {
field1: 1,
field2: "common",
special: 203,
};

// error
const works2: Lookup<"common"> = {
field1: 5,
field2: "something",
special: 32,
~~~~~~~
!!! error TS2353: Object literal may only specify known properties, and 'special' does not exist in type 'Lookup<"common">'.
};

// ok
const works3: Lookup<K> = {
special: undefined,
field1: undefined,
field2: undefined,
};

// error
const works4: Lookup<K> = {
~~~~~~
!!! error TS2322: Type '{ field1: undefined; field2: undefined; special: 32; }' is not assignable to type 'Lookup<K>'.
field1: undefined,
field2: undefined,
special: 32,
};
}

export {};

Loading