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

ES6-style import declarations don't work in TypeScript #88

Closed
TedDriggs opened this issue Oct 3, 2017 · 5 comments
Closed

ES6-style import declarations don't work in TypeScript #88

TedDriggs opened this issue Oct 3, 2017 · 5 comments

Comments

@TedDriggs
Copy link

Environment: Typescript@2.5.2

import ipaddr from 'ipaddr.js';

ipaddr.parse("1.2.3.4"); // error; cannot call function 'parse' of undefined
const ipaddr = require('ipaddr.js');

ipaddr.parse("1.2.3.4"); // works

The tsconfig.json used for this was:

{
    "compileOnSave": true,
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "target": "es2016",
        "strictNullChecks": true
    },
    "exclude": [
        "node_modules"
    ]
}

Mostly, I was surprised that the library isn't recognized as a module unless imported via require. This also makes declaring a .d.ts file more difficult; the one I'd started worked with the ES6-style import but not with the require declaration.

@whitequark
Copy link
Owner

I know basically nothing about post-2012 JavaScript. Help me out?

@TedDriggs
Copy link
Author

I'm not 100% sure what's happening yet either; still investigating.

@mooyoul
Copy link

mooyoul commented Nov 7, 2017

  1. import module like this way:
import * as ipaddr from 'ipaddr.js';
  1. declare module like below:
// this filename should have .d.ts extension
declare module 'ipaddr.js' {
  type IPVersion = "ipv4" |  "ipv6";

  interface IPAddr {
    type: () => IPVersion;
    octet: number[];
  }

  interface IPAddrModule {
    parse: (ip: string) => IPAddr
  }

  const module: IPAddrModule;
  export = module;
}

@raijinsetsu
Copy link
Contributor

I opened #93 to get type definitions added to this project. Email me if you want a more complete definition than above.

@whitequark
Copy link
Owner

#93 should have fixed this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants