ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

`ExportedDeclarations` type union is missing type `BindingElement`

Open velut opened this issue 1 year ago • 0 comments

Describe the bug

Version: 21.0.1

The ExportedDeclarations type union is missing the type BindingElement which can be returned when an export declaration uses object or array destructuring, for example:

// Export with object destructuring.
export const {
    foo,
    bar: baz,
    ...qux
} = {
    foo: "hello",
    bar: 42,
    quxA: 1, quxB: 2, quxC: 3
} as const;

// Export with array destructuring.
export const [aaa, bbb, ...[ccc, ddd]] = [1, 2, 3, 4] as const;

To Reproduce

import { Project } from "ts-morph";

const project = new Project({ useInMemoryFileSystem: true });

const sourceFile = project.createSourceFile(
  "test.ts",
  `
// Export with object destructuring.
export const {
    foo,
    bar: baz,
    ...qux
} = {
    foo: "hello",
    bar: 42,
    quxA: 1, quxB: 2, quxC: 3
} as const;

// Export with array destructuring.
export const [aaa, bbb, ...[ccc, ddd]] = [1, 2, 3, 4] as const;
`,
);

for (const [exportName, declarations] of sourceFile.getExportedDeclarations()) {
  for (const declaration of declarations) {
    console.log({
      exportName,
      kind: declaration.getKindName(), // All declarations are of kind `BindingElement`
      type: declaration.getType().getText(),
    });
  }
}

Output:

{
  exportName: "foo",
  kind: "BindingElement",
  type: "\"hello\"",
}
{
  exportName: "baz",
  kind: "BindingElement",
  type: "42",
}
{
  exportName: "qux",
  kind: "BindingElement",
  type: "{ quxA: 1; quxB: 2; quxC: 3; }",
}
{
  exportName: "aaa",
  kind: "BindingElement",
  type: "1",
}
{
  exportName: "bbb",
  kind: "BindingElement",
  type: "2",
}
{
  exportName: "ccc",
  kind: "BindingElement",
  type: "3",
}
{
  exportName: "ddd",
  kind: "BindingElement",
  type: "4",
}

Expected behavior

ExportedDeclarations type union should include type BindingElement.

Thank you for your time!

velut avatar Feb 01 '24 23:02 velut