Skip to content

Fix assignability of unconstrained T[K] #23768

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

Merged
merged 3 commits into from
Apr 29, 2018
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
33 changes: 11 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10670,23 +10670,8 @@ namespace ts {
}
}

if (source.flags & TypeFlags.TypeParameter) {
let constraint = getConstraintForRelation(<TypeParameter>source);
// A type parameter with no constraint is not related to the non-primitive object type.
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
if (!constraint || constraint.flags & TypeFlags.Any) {
constraint = emptyObjectType;
}
// Report constraint errors only if the constraint is not the empty object type
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
errorInfo = saveErrorInfo;
return result;
}
}
}
else if (source.flags & TypeFlags.IndexedAccess) {
if (target.flags & TypeFlags.IndexedAccess) {
if (source.flags & TypeFlags.TypeVariable) {
if (source.flags & TypeFlags.IndexedAccess && target.flags & TypeFlags.IndexedAccess) {
// A type S[K] is related to a type T[J] if S is related to T and K is related to J.
if (result = isRelatedTo((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType, reportErrors)) {
result &= isRelatedTo((<IndexedAccessType>source).indexType, (<IndexedAccessType>target).indexType, reportErrors);
Expand All @@ -10696,11 +10681,15 @@ namespace ts {
return result;
}
}
// A type S[K] is related to a type T if C is related to T, where C is the
// constraint of S[K].
const constraint = getConstraintForRelation(<IndexedAccessType>source);
if (constraint) {
if (result = isRelatedTo(constraint, target, reportErrors)) {
let constraint = getConstraintForRelation(<TypeParameter>source);
// A type variable with no constraint is not related to the non-primitive object type.
if (constraint || !(target.flags & TypeFlags.NonPrimitive)) {
if (!constraint || constraint.flags & TypeFlags.Any) {
constraint = emptyObjectType;
}
// Report constraint errors only if the constraint is not the empty object type
const reportConstraintErrors = reportErrors && constraint !== emptyObjectType;
if (result = isRelatedTo(constraint, target, reportConstraintErrors)) {
errorInfo = saveErrorInfo;
return result;
}
Expand Down
70 changes: 70 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ function f90<T extends S2, K extends keyof S2>(x1: S2[keyof S2], x2: T[keyof S2]
x4.length;
}

function f91<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]) {
let a: {};
a = x;
a = y;
a = z;
}

function f92<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]) {
let a: {} | null | undefined;
a = x;
a = y;
a = z;
}

// Repros from #12011

class Base {
Expand Down Expand Up @@ -592,6 +606,27 @@ type DynamicDBRecord<Flag extends string> = ({ dynamicField: number } | { dynami
function getFlagsFromDynamicRecord<Flag extends string>(record: DynamicDBRecord<Flag>, flags: Flag[]) {
return record[flags[0]];
}

// Repro from #21368

interface I {
foo: string;
}

declare function take<T>(p: T): void;

function fn<T extends I, K extends keyof T>(o: T, k: K) {
take<{} | null | undefined>(o[k]);
take<any>(o[k]);
}

// Repro from #23133

class Unbounded<T> {
foo(x: T[keyof T]) {
let y: {} | undefined | null = x;
}
}


//// [keyofAndIndexedAccess.js]
Expand Down Expand Up @@ -830,6 +865,18 @@ function f90(x1, x2, x3, x4) {
x3.length;
x4.length;
}
function f91(x, y, z) {
var a;
a = x;
a = y;
a = z;
}
function f92(x, y, z) {
var a;
a = x;
a = y;
a = z;
}
// Repros from #12011
var Base = /** @class */ (function () {
function Base() {
Expand Down Expand Up @@ -986,6 +1033,19 @@ function getFlagsFromSimpleRecord(record, flags) {
function getFlagsFromDynamicRecord(record, flags) {
return record[flags[0]];
}
function fn(o, k) {
take(o[k]);
take(o[k]);
}
// Repro from #23133
var Unbounded = /** @class */ (function () {
function Unbounded() {
}
Unbounded.prototype.foo = function (x) {
var y = x;
};
return Unbounded;
}());


//// [keyofAndIndexedAccess.d.ts]
Expand Down Expand Up @@ -1126,6 +1186,8 @@ declare type S2 = {
b: string;
};
declare function f90<T extends S2, K extends keyof S2>(x1: S2[keyof S2], x2: T[keyof S2], x3: S2[K], x4: T[K]): void;
declare function f91<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]): void;
declare function f92<T, K extends keyof T>(x: T, y: T[keyof T], z: T[K]): void;
declare class Base {
get<K extends keyof this>(prop: K): this[K];
set<K extends keyof this>(prop: K, value: this[K]): void;
Expand Down Expand Up @@ -1298,3 +1360,11 @@ declare type DynamicDBRecord<Flag extends string> = ({
dynamicField: string;
}) & DBBoolTable<Flag>;
declare function getFlagsFromDynamicRecord<Flag extends string>(record: DynamicDBRecord<Flag>, flags: Flag[]): DynamicDBRecord<Flag>[Flag];
interface I {
foo: string;
}
declare function take<T>(p: T): void;
declare function fn<T extends I, K extends keyof T>(o: T, k: K): void;
declare class Unbounded<T> {
foo(x: T[keyof T]): void;
}
Loading