feat(language-service): completions support for tuple array (#33928)
PR Close #33928
This commit is contained in:
@ -241,7 +241,8 @@ export class AstType implements AstVisitor {
|
||||
visitKeyedRead(ast: KeyedRead): Symbol {
|
||||
const targetType = this.getType(ast.obj);
|
||||
const keyType = this.getType(ast.key);
|
||||
const result = targetType.indexed(keyType);
|
||||
const result = targetType.indexed(
|
||||
keyType, ast.key instanceof LiteralPrimitive ? ast.key.value : undefined);
|
||||
return result || this.anyType;
|
||||
}
|
||||
|
||||
|
@ -116,9 +116,12 @@ export interface Symbol {
|
||||
|
||||
/**
|
||||
* Return the type of the expression if this symbol is indexed by `argument`.
|
||||
* Sometimes we need the key of arguments to get the type of the expression, for example
|
||||
* in the case of tuples (`type Example = [string, number]`).
|
||||
* [string, number]).
|
||||
* If the symbol cannot be indexed, this method should return `undefined`.
|
||||
*/
|
||||
indexed(argument: Symbol): Symbol|undefined;
|
||||
indexed(argument: Symbol, key?: any): Symbol|undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -349,4 +352,4 @@ export interface SymbolQuery {
|
||||
* Return the span of the narrowest non-token node at the given location.
|
||||
*/
|
||||
getSpanAt(line: number, column: number): Span|undefined;
|
||||
}
|
||||
}
|
||||
|
@ -284,7 +284,7 @@ class TypeWrapper implements Symbol {
|
||||
return selectSignature(this.tsType, this.context, types);
|
||||
}
|
||||
|
||||
indexed(argument: Symbol): Symbol|undefined {
|
||||
indexed(argument: Symbol, value: any): Symbol|undefined {
|
||||
const type = argument instanceof TypeWrapper ? argument : argument.type;
|
||||
if (!(type instanceof TypeWrapper)) return;
|
||||
|
||||
@ -292,7 +292,14 @@ class TypeWrapper implements Symbol {
|
||||
switch (typeKind) {
|
||||
case BuiltinType.Number:
|
||||
const nType = this.tsType.getNumberIndexType();
|
||||
return nType && new TypeWrapper(nType, this.context);
|
||||
if (nType) {
|
||||
// get the right tuple type by value, like 'var t: [number, string];'
|
||||
if (nType.isUnion()) {
|
||||
return new TypeWrapper(nType.types[value], this.context);
|
||||
}
|
||||
return new TypeWrapper(nType, this.context);
|
||||
}
|
||||
return undefined;
|
||||
case BuiltinType.String:
|
||||
const sType = this.tsType.getStringIndexType();
|
||||
return sType && new TypeWrapper(sType, this.context);
|
||||
|
Reference in New Issue
Block a user