Skip to content

Minor fixes for 'keyof T' and indexed acces types #12389

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
Nov 20, 2016
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
20 changes: 12 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6037,19 +6037,20 @@ namespace ts {
const id = objectType.id + "," + indexType.id;
return indexedAccessTypes[id] || (indexedAccessTypes[id] = createIndexedAccessType(objectType, indexType));
}
const apparentType = getApparentType(objectType);
if (indexType.flags & TypeFlags.Union && !(indexType.flags & TypeFlags.Primitive)) {
const apparentObjectType = getApparentType(objectType);
const apparentIndexType = indexType.flags & TypeFlags.Index ? stringOrNumberType : indexType;
if (apparentIndexType.flags & TypeFlags.Union && !(apparentIndexType.flags & TypeFlags.Primitive)) {
const propTypes: Type[] = [];
for (const t of (<UnionType>indexType).types) {
const propType = getPropertyTypeForIndexType(apparentType, t, accessNode, /*cacheSymbol*/ false);
for (const t of (<UnionType>apparentIndexType).types) {
const propType = getPropertyTypeForIndexType(apparentObjectType, t, accessNode, /*cacheSymbol*/ false);
if (propType === unknownType) {
return unknownType;
}
propTypes.push(propType);
}
return getUnionType(propTypes);
}
return getPropertyTypeForIndexType(apparentType, indexType, accessNode, /*cacheSymbol*/ true);
return getPropertyTypeForIndexType(apparentObjectType, apparentIndexType, accessNode, /*cacheSymbol*/ true);
}

function getTypeFromIndexedAccessTypeNode(node: IndexedAccessTypeNode) {
Expand Down Expand Up @@ -7123,7 +7124,10 @@ namespace ts {

if (source.flags & TypeFlags.Index) {
// A keyof T is related to a union type containing both string and number
if (maybeTypeOfKind(target, TypeFlags.String) && maybeTypeOfKind(target, TypeFlags.Number)) {
const related = relation === comparableRelation ?
maybeTypeOfKind(target, TypeFlags.String | TypeFlags.Number) :
maybeTypeOfKind(target, TypeFlags.String) && maybeTypeOfKind(target, TypeFlags.Number);
if (related) {
return Ternary.True;
}
}
Expand Down Expand Up @@ -14254,7 +14258,7 @@ namespace ts {
if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, TypeFlags.StringLike | TypeFlags.NumberLike | TypeFlags.ESSymbol)) {
error(left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol);
}
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeParameter)) {
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeParameter | TypeFlags.IndexedAccess)) {
error(right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
return booleanType;
Expand Down Expand Up @@ -17148,7 +17152,7 @@ namespace ts {
const rightType = checkNonNullExpression(node.expression);
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
// in this case error about missing name is already reported - do not report extra one
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeParameter)) {
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeParameter | TypeFlags.IndexedAccess)) {
error(node.expression, Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter);
}

Expand Down
84 changes: 84 additions & 0 deletions tests/baselines/reference/keyofAndIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,46 @@ function f40(c: C) {
let z: Z = c["z"];
}

function f50<T>(k: keyof T, s: string, n: number) {
const x1 = s as keyof T;
const x2 = n as keyof T;
const x3 = k as string;
const x4 = k as number;
const x5 = k as string | number;
}

function f51<T, K extends keyof T>(k: K, s: string, n: number) {
const x1 = s as keyof T;
const x2 = n as keyof T;
const x3 = k as string;
const x4 = k as number;
const x5 = k as string | number;
}

function f52<T>(obj: { [x: string]: boolean }, k: keyof T, s: string, n: number) {
const x1 = obj[s];
const x2 = obj[n];
const x3 = obj[k];
}

function f53<T, K extends keyof T>(obj: { [x: string]: boolean }, k: K, s: string, n: number) {
const x1 = obj[s];
const x2 = obj[n];
const x3 = obj[k];
}

function f54<T>(obj: T, key: keyof T) {
for (let s in obj[key]) {
}
const b = "foo" in obj[key];
}

function f55<T, K extends keyof T>(obj: T, key: K) {
for (let s in obj[key]) {
}
const b = "foo" in obj[key];
}

// Repros from #12011

class Base {
Expand Down Expand Up @@ -329,6 +369,40 @@ function f40(c) {
var y = c["y"];
var z = c["z"];
}
function f50(k, s, n) {
var x1 = s;
var x2 = n;
var x3 = k;
var x4 = k;
var x5 = k;
}
function f51(k, s, n) {
var x1 = s;
var x2 = n;
var x3 = k;
var x4 = k;
var x5 = k;
}
function f52(obj, k, s, n) {
var x1 = obj[s];
var x2 = obj[n];
var x3 = obj[k];
}
function f53(obj, k, s, n) {
var x1 = obj[s];
var x2 = obj[n];
var x3 = obj[k];
}
function f54(obj, key) {
for (var s in obj[key]) {
}
var b = "foo" in obj[key];
}
function f55(obj, key) {
for (var s in obj[key]) {
}
var b = "foo" in obj[key];
}
// Repros from #12011
var Base = (function () {
function Base() {
Expand Down Expand Up @@ -454,6 +528,16 @@ declare class C {
private z;
}
declare function f40(c: C): void;
declare function f50<T>(k: keyof T, s: string, n: number): void;
declare function f51<T, K extends keyof T>(k: K, s: string, n: number): void;
declare function f52<T>(obj: {
[x: string]: boolean;
}, k: keyof T, s: string, n: number): void;
declare function f53<T, K extends keyof T>(obj: {
[x: string]: boolean;
}, k: K, s: string, n: number): void;
declare function f54<T>(obj: T, key: keyof T): void;
declare function f55<T, K extends keyof T>(obj: T, key: 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
Loading