Skip to content

Literal types cannot be used in JSDoc export default { } syntax #47946

Closed
@nicojs

Description

@nicojs

Bug Report

There seems to be an issue with regards to literal types and JSDoc when using export default {} syntax.

🔎 Search Terms

"jsdoc" and "export default"

Found this related issue: #45233, and Ryan asked me to create a new issue

🕗 Version & Regression Information

It seems to have been introduced in TS@4.5. I don't think TS 4.4 had support for typed export default {} with JSDoc.

  • This changed between versions 4.4 and 4.5

⏯ Playground Link

Not possible I think (playground doesn't have plain "js" support?)

💻 Code

In a file "foo.js":

// @ts-check
/**
 * @typedef Foo
 * @property {string} bar
 * @property {'qux'} baz
 */

/** @type {Foo} */
export default {
    bar: '42',
    baz: 'qux' // 👈 This is NOT ok
      // ^^^^^  Type 'string' is not assignable to type '"qux"'
}

/** @type {Foo} */
export const foo = {
    bar: '42',
    baz: 'qux' // 👈 This is OK
}

🙁 Actual behavior

Type 'string' is not assignable to type '"qux"'

🙂 Expected behavior

no compile error

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: JSDocRelates to JSDoc parsing and type generation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions