Skip to content

Declaration emit from javascript doesn't respect optional properties in function parametersΒ #42099

Closed
@dbellavista

Description

@dbellavista

Bug Report

πŸ”Ž Search Terms

  • javascript declaration
  • optional object parameters

πŸ•— Version & Regression Information

  • typescript 4.0
  • typescript 4.1
  • typescript@beta
  • This changed between versions 3.x and 4.x

⏯ Playground Link

Playground link with relevant code (check .D.TS tab)

πŸ’» Code

jsconfig.json
{
  "compilerOptions": {
    "module": "es6",
    "moduleResolution": "node",
    "target": "es2020",
    "checkJs": true,
    "declaration": true,
    "noEmit": false,
    "emitDeclarationOnly": true
  }
}
test.js
/**
 * test
 *
 * @public
 * @param {object} opts
 * @param {number} opts.a
 * @param {number} [opts.b]
 * @param {number} [opts.c]
 * @returns {number}
 */
function test({a, b, c}) {
  return a + b + c;
}
npx tsc  -p jsconfig.json

πŸ™ Actual behavior

The definition file doesn't mark b and c as optional

test.d.ts
/**
 * test
 *
 * @public
 * @param {object} opts
 * @param {number} opts.a
 * @param {number} [opts.b]
 * @param {number} [opts.c]
 * @returns {number}
 */
declare function test({ a, b, c }: {
    a: number;
    b: number;
    c: number;
}): number;

πŸ™‚ Expected behavior

The definition file should mark b and c as optional (so that projects importing the definitions will be checked properly)

test.d.ts
/**
 * test
 *
 * @public
 * @param {object} opts
 * @param {number} opts.a
 * @param {number} [opts.b]
 * @param {number} [opts.c]
 * @returns {number}
 */
declare function test({ a, b, c }: {
    a: number;
    b?: number;
    c?: number;
}): number;

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Declaration EmitThe issue relates to the emission of d.ts filesDomain: JSDocRelates to JSDoc parsing and type generation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions