JSDoc:Object<string, T> creates index signature

And `Object<number, T>` creates a numeric index signature. Other uses
still create `any` as before.
This commit is contained in:
Nathan Shively-Sanders 2017-07-17 16:14:42 -07:00
parent de9a67f2f3
commit 10a91c5426
4 changed files with 47 additions and 2 deletions

View File

@ -6872,6 +6872,17 @@ namespace ts {
function getPrimitiveTypeFromJSDocTypeReference(node: TypeReferenceNode): Type {
if (isIdentifier(node.typeName)) {
if (node.typeName.text === "Object") {
if (node.typeArguments && node.typeArguments.length === 2) {
const from = getTypeFromTypeNode(node.typeArguments[0]);
const to = getTypeFromTypeNode(node.typeArguments[1]);
let index = createIndexInfo(to, /*isReadonly*/ false);
if (from === stringType || from === numberType) {
return createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, from === stringType ? index : undefined, from === numberType ? index : undefined)
}
}
return anyType;
}
switch (node.typeName.text) {
case "String":
return stringType;
@ -6885,8 +6896,6 @@ namespace ts {
return undefinedType;
case "Null":
return nullType;
case "Object":
return anyType;
case "Function":
case "function":
return globalFunctionType;

View File

@ -0,0 +1,13 @@
=== tests/cases/conformance/jsdoc/indices.js ===
/** @type {Object.<string, number>} */
var o1;
>o1 : Symbol(o1, Decl(indices.js, 1, 3))
/** @type {Object.<number, boolean>} */
var o2;
>o2 : Symbol(o2, Decl(indices.js, 3, 3))
/** @type {Object.<boolean, string>} */
var o3;
>o3 : Symbol(o3, Decl(indices.js, 5, 3))

View File

@ -0,0 +1,13 @@
=== tests/cases/conformance/jsdoc/indices.js ===
/** @type {Object.<string, number>} */
var o1;
>o1 : { [x: string]: number; }
/** @type {Object.<number, boolean>} */
var o2;
>o2 : { [x: number]: boolean; }
/** @type {Object.<boolean, string>} */
var o3;
>o3 : any

View File

@ -0,0 +1,10 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @Filename: indices.js
/** @type {Object.<string, number>} */
var o1;
/** @type {Object.<number, boolean>} */
var o2;
/** @type {Object.<boolean, string>} */
var o3;