Skip to content

Commit 65ff3fd

Browse files
committed
Condier keysof types string like, dont get index types
1 parent 9613ca8 commit 65ff3fd

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

src/compiler/checker.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -5151,21 +5151,11 @@ namespace ts {
51515151
}
51525152

51535153
function resolveKeysQueryType(type: KeysQueryType): Type {
5154-
// Get index types to include in the union if need be
5155-
let indexTypes: Type[];
5156-
const numberIndex = getIndexInfoOfType(type.baseType, IndexKind.Number);
5157-
const stringIndex = getIndexInfoOfType(type.baseType, IndexKind.String);
5158-
if (numberIndex) {
5159-
indexTypes = [numberType];
5160-
}
5161-
if (stringIndex) {
5162-
indexTypes ? indexTypes.push(stringType) : indexTypes = [stringType];
5163-
}
51645154
// Skip any essymbol members and remember to unescape the identifier before making a type from it
5165-
const memberTypes = concatenate(indexTypes, map(filter(getPropertiesOfType(type.baseType),
5155+
const memberTypes = map(filter(getPropertiesOfType(type.baseType),
51665156
symbol => !startsWith(symbol.name, "__@")),
51675157
symbol => getLiteralTypeForText(TypeFlags.StringLiteral, unescapeIdentifier(symbol.name))
5168-
));
5158+
);
51695159
return memberTypes ? getUnionType(memberTypes) : neverType;
51705160
}
51715161

@@ -13404,7 +13394,7 @@ namespace ts {
1340413394
contextualType = apparentType;
1340513395
}
1340613396
if (contextualType.flags & TypeFlags.KeysQuery) {
13407-
return true;
13397+
return type === stringType;
1340813398
}
1340913399
if (type.flags & TypeFlags.String) {
1341013400
return maybeTypeOfKind(contextualType, TypeFlags.StringLiteral);

src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ namespace ts {
22922292
Intrinsic = Any | String | Number | Boolean | BooleanLiteral | ESSymbol | Void | Undefined | Null | Never,
22932293
/* @internal */
22942294
Primitive = String | Number | Boolean | Enum | ESSymbol | Void | Undefined | Null | Literal,
2295-
StringLike = String | StringLiteral,
2295+
StringLike = String | StringLiteral | KeysQuery,
22962296
NumberLike = Number | NumberLiteral | Enum | EnumLiteral,
22972297
BooleanLike = Boolean | BooleanLiteral,
22982298
EnumLike = Enum | EnumLiteral,

tests/cases/conformance/types/keysof/simpleKeysofTest.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ function disambiguate2(): keysof ({a}) {return "a";}
1616
interface FooBar {
1717
foo: "yes";
1818
bar: "no";
19+
[index: string]: string; // Remove when the indexer is patched to passthru unions
1920
}
2021

21-
function pick(thing: FooBar, member: keysof FooBar): typeof member {
22+
function pick(thing: FooBar, member: keysof FooBar) {
2223
return thing[member];
2324
}
2425

@@ -32,10 +33,12 @@ const x = pick2({a: "", b: 0}, realA);
3233
const xx = pick2({a: "", b: 0}, "a");
3334
const item = {0: "yes", 1: "no"};
3435
const xxx = pick2(item, "0");
35-
const xxxx = pick2(item, 0);
36-
item["0"].charCodeAt(0);
37-
item[0].charCodeAt(0);
3836

39-
function pick3<U, T extends keysof U>(obj: U, key: T) {
40-
key
41-
}
37+
function annotate<U, T extends keysof U>(obj: U, key: T): U & {annotation: T} {
38+
const ret = obj as U & {annotation: T};
39+
if (key === "annotation") return ret; // Already annotated
40+
ret.annotation = key;
41+
return ret;
42+
}
43+
44+
annotate({a: "things", b: "stuff"}, "b").annotation === "b";

0 commit comments

Comments
 (0)