microsoft / tslib

Runtime library for TypeScript helpers.
BSD Zero Clause License
1.25k stars 126 forks source link

declare module `'tslib'` #16

Closed billderose-zz closed 7 years ago

billderose-zz commented 7 years ago

Right now, tslib.d.ts does not contain a module declaration for 'tslib' and instead declares the functions in the global namespace. As of TS 2.1.4, when I enable the --noEmitHelpers and --importHelpers compiler flags, my build fails because it cannot find the required 'tslib' module.

I'm opening an issue because there's probably a reason this hasn't been done already, but wouldn't it be possible to do something like:

// ... existing global typing defs
declare module 'tslib' {
  export function __extends(d: Function, b: Function): void;
  export function __assign(t: any, ...sources: any[]): any;
  export function __rest(t: any, propertyNames: string[]): any;
  export function __decorate(decorators: Function[], target: any, key?: string | symbol, desc?: any): any;
  export function __param(paramIndex: number, decorator: Function): Function;
  export function __metadata(metadataKey: any, metadataValue: any): Function;
  export function __awaiter(thisArg: any, _arguments: any, P: Function, generator: Function): any;
  export function __generator(thisArg: any, body: Function): any;
}

@billti @rbuckton @sandersn for visibility

rbuckton commented 7 years ago

There should be no need for a declare module "tslib", as tslib.d.ts is the module and exports at the top level. Also package.json declares tslib.d.ts as the typings for the package. What issue are you seeing?

billderose-zz commented 7 years ago

@rbuckton thanks for the response. I think the issue stems from the fact that I'm consuming tslib via bower (https://github.com/Draccoz/tslib). Running bower install tslib --save installs into my vendor directory but at compile time, I get: error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found. My tsconfig.json looks like:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "importHelpers": true,
        "jsx": "react",
        "module": "amd",
        "noEmitHelpers": true,
        "target": "es5",
        "typeRoots": [
            "./typings/**/*.d.ts",
            "../vendor/tslib/tslib.d.ts"
        ]
    }
}

If I copy/paste the declare module 'tslib' {...} from my initial comment, into the typings directory then the build goes through with no complaints. I'm guessing this is a non-standard way of consuming the package?

rbuckton commented 7 years ago

I think part of the issue is that your "typeRoots" definition is incorrect. The "typeRoots" field should point to folders only, and does not support globs. Please refer to www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types for more information.

billderose-zz commented 7 years ago

Interesting, and good to know. When I change my tsconfig.json to

{
...
"typeRoots": [ "./typings", "../vendor/tslib"]
}

I get more compiler errors about not being able to find all the modules whose typings are declared in ./typings, e.g. error TS2688: Cannot find type definition file for 'node'.. I also still get the error about not being able to find the tslib module: error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found. I'm now wondering if this is due to a mismatch in the module resolution required for the respective modules??

rbuckton commented 7 years ago

You should be able to accomplish this via path mapping:

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "tslib": ["../vendor/tslib/tslib.d.ts"] }
  }
}
mhegazy commented 7 years ago

Also updated in the readme: https://github.com/Microsoft/tslib/blob/master/README.md#for-bower-users