Skip to content

Wrongly generated .d.ts #1704

Closed
Closed
@jerrygreen

Description

@jerrygreen

I've found a good issue here, and figured out that I can actually generate .d.ts by setting tsdFile option in config, and use AssemblyScript types inside TypeScript! Which is absolutely cool, but I found that automatically it actually generates slightly wrong hierarchy.

I have further AssemblyScript code:

export function add(a: i32, b: i32): i32 {
  return a + b
}

And a wasm module as a react component:

const AsmComponent = dynamic({
  loader: async () => {
    const wasmModule = await import('../../../asm/build/optimized') // <- imports `.wasm`

    return (props) => <>{wasmModule.add(1, 2)}</>
  },
  ssr: false,
})

And I render it in React like so:

          <p>Execute function from wasm module:</p>
          <p>
            1 + 2 = <AsmComponent />
          </p>

And it works:

image

So it works, but look at my types:

Property 'add' does not exist on type 'typeof import("c:/Users/Jerry/projects/project-x/asm/build/optimized")'

image

Now I can fix these types by replacing asmModule.add into asmModule.default.add:

image

But if I now run actual application, "default" doesn't actually have "add" function:

image

My automatically generated optimized.d.ts looks like so:

declare module ASModule {
  type i8 = number;
  type i16 = number;
  type i32 = number;
  type i64 = bigint;
  type isize = number;
  type u8 = number;
  type u16 = number;
  type u32 = number;
  type u64 = bigint;
  type usize = number;
  type f32 = number;
  type f64 = number;
  type bool = boolean | number;
  export function add(a: i32, b: i32): i32;
}
export default ASModule;

If I manually remove the namespace, leaving bare types like so:

type i8 = number;
type i16 = number;
type i32 = number;
type i64 = bigint;
type isize = number;
type u8 = number;
type u16 = number;
type u32 = number;
type u64 = bigint;
type usize = number;
type f32 = number;
type f64 = number;
type bool = boolean | number;
export function add(a: i32, b: i32): i32;

Then both types and actual code go aligned, and both working!

Pls can someone change how .d.ts file is generated, and generate it without a namespace?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions