Skip to content

jsdoc: defining object format with @typedef then using it with @type doesn't seem to work #15812

Closed
@balupton

Description

@balupton

TypeScript Version: 2.3.2

Code

example repository: https://github.com/balupton/typescript-typedef-object-issue

source/index.js:

'use strict'

/**
 * @typedef {Object.<string, function(*):boolean>} TypeMap
 */

/**
 * Get the type of the value in lowercase
 * @param {*} value
 * @param {TypeMap} [customTypeMap] a custom type map
 * @returns {?string}
 */
function getType (value, customTypeMap) {
   // Cycle through our type map
   for (const key in customTypeMap) {
      if (customTypeMap.hasOwnProperty(key)) {
         if (customTypeMap[key](value)) {
            return key
         }
      }
   }

   // No type was successful
   return null
}

/**
 * Get the type of the value in lowercase
 * @param {*} value
 * @param {Object.<string, function(*):boolean>} [customTypeMap] a custom type map
 * @returns {?string}
 */
function getTypeWorkaround (value, customTypeMap) {
   // Cycle through our type map
   for (const key in customTypeMap) {
      if (customTypeMap.hasOwnProperty(key)) {
         if (customTypeMap[key](value)) {
            return key
         }
      }
   }

   // No type was successful
   return null
}

tsconfig.json:

{
   "include": [
      "source/*"
   ],
   "compileOnSave": true,
   "compilerOptions": {
      "allowJs": true,
      "checkJs": true,
      "strict": true,
      "alwaysStrict": true,
      "module": "commonjs",
      "newLine": "LF",
      "target": "es2015",
      "outDir": "es2015",
      "lib": [
         "esnext"
      ],
      "rootDir": ".",
      "rootDirs": [
         "."
      ]
   }
}

Expected behavior:
Expected that getType would use the TypeMap type definition correctly.

Actual behavior:
I get this error message on compile:

> tsc

source/index.js(17,14): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.

VSCode Screenshots:

img_4826

img_4827

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions