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

Is it possible to override type definitions for browser? #16839

Closed
daprahamian opened this issue Jun 29, 2017 · 3 comments
Closed

Is it possible to override type definitions for browser? #16839

daprahamian opened this issue Jun 29, 2017 · 3 comments

Comments

@daprahamian
Copy link

Hi,

I am currently working on a project that exports two different packages depending on platform:

example package.json

{
  "name": "my-lib",
  "main": "lib/commonjs/my-lib.js",
  "browser": "lib/umd/my-lib.js"
  ...
}

My question is: is it possible to override the "types" field in the package.json to make the module resolve to the main types in node, and the browser types in a web browser?

Thanks

@ikatyang
Copy link
Contributor

I haven't used browser field before, but It seems you have to add the umd declaration in your generated my-lib.d.ts file, for example:

(package.json)

{
  "name": "my-lib",
  "main": "lib/commonjs/my-lib.js",
  "browser": "lib/umd/my-lib.js",
  "types": "lib/my-lib.d.ts"
}

(lib/my-lib.d.ts)

export const example = 1;
export as namespace NS; // <--- mark this declaration file as umd compatible

(somewhere/you/want/to/use/umd.ts)

/// <reference types="my-lib" />
NS.example; //=> 1

(somewhere/you/want/to/use/commonjs.ts)

import * as MyLib from 'my-lib';
MyLib.example; //=> 1

@blakeembrey
Copy link
Contributor

No, it's not. See #7753.

@daprahamian
Copy link
Author

Closing in favor of #7753

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants