Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`umd` format `export as namespace MyLib` does not work without `@types/*` #30199

Closed
JounQin opened this issue Mar 3, 2019 · 4 comments
Closed

`umd` format `export as namespace MyLib` does not work without `@types/*` #30199

JounQin opened this issue Mar 3, 2019 · 4 comments

Comments

@JounQin
Copy link

@JounQin JounQin commented Mar 3, 2019

TypeScript Version: 3.4.0-dev.20190302

Search Terms: export as namespace, umd

Code

// file: node_modules/my-lib/index.d.ts , manually added

export = MyLib

export as namespace MyLib

declare namespace MyLib {
  class A {}
}

// file: my-lib.test.ts
const x = new MyLib.A() // umd MyLib does not work here

Expected behavior:

export as namespace works for umd format

Actual behavior:

There is an error: error TS2304: Cannot find name 'MyLib'.

But if I move node_modules/my-lib/index.d.ts to node_modules/@types/my-lib/index.d.ts, there will be no error any more.

Playground Link:

Related Issues:

@RyanCavanaugh

This comment has been minimized.

Copy link
Member

@RyanCavanaugh RyanCavanaugh commented Mar 5, 2019

What does "manually added" mean? Is there a tsconfig file? What is your tsc commandline?

@JounQin

This comment has been minimized.

Copy link
Author

@JounQin JounQin commented Mar 6, 2019

@RyanCavanaugh my-lib is a test module, which I manually created in node_modules directory.

And my tsc command is simple tsc my-lib.test

@JounQin

This comment has been minimized.

Copy link
Author

@JounQin JounQin commented Jun 19, 2019

@JounQin

This comment has been minimized.

Copy link
Author

@JounQin JounQin commented Jun 20, 2019

I've found out how to make it work: namespaced file should be in a specific .d.ts file with root level declare namespace, and then declare module in another .d.ts file.

// placeholder.js.d.ts
declare namespace placeholder {
  type PlaceholderOptions = Partial<{
    size: string;
    bgcolor: string;
    color: string;
    text: string;
    fstyle: string;
    fweight: string;
    fsize: string;
    ffamily: string;
  }>;

  interface Placeholder {
    getCanvas(options?: PlaceholderOptions): HTMLCanvasElement;
    getData(options?: PlaceholderOptions): string;
    render(force?: string): void;
  }
}

declare const placeholder: placeholder.Placeholder;

export = placeholder;

export as namespace placeholder;
// typings.d.ts
declare module 'placeholder.js' {
  export = placeholder;
}
@JounQin JounQin closed this Jun 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.