Skip to content

Commit 01b0744

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

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

Diff for: src/compiler/checker.ts

+2-12
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

Diff for: 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,

Diff for: tests/cases/conformance/types/keysof/simpleKeysofTest.ts

+9-5
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;
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,13 @@ 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);
3636
item["0"].charCodeAt(0);
3737
item[0].charCodeAt(0);
3838

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

0 commit comments

Comments
 (0)