Skip to content

Generating __awaiter in all files even if no async/await is present #5564

Closed
@tjoskar

Description

@tjoskar

Hi,

If I compile the following code:

class Index {
    async someMethod() {
        await new Promise(resolve => resolve());
    }
}

It generates a helper function __awaiter and adds it to the output file, which is fine.

However, if I add another ts file, the output file will contain the __awaiter function as well, even if it doesn't contain any async/await method.

Exemple:

index.ts:

class Index {
    async someMethod() {
        await new Promise(resolve => resolve());
    }
}

a.ts:

let a;

Output:
index.js:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
class Index {
    someMethod() {
        return __awaiter(this, void 0, Promise, function* () {
            yield new Promise(resolve => resolve());
        });
    }
}

a.js:

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
    return new Promise(function (resolve, reject) {
        generator = generator.call(thisArg, _arguments);
        function cast(value) { return value instanceof Promise && value.constructor === Promise ? value : new Promise(function (resolve) { resolve(value); }); }
        function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
        function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
        function step(verb, value) {
            var result = generator[verb](value);
            result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
        }
        step("next", void 0);
    });
};
let a;

Is this by design?
This is not a big issue from my part but I'm working on a quite big project with lots of files and this adds about 604 B for each file (if I remove the spaces), and it adds a lot of noise.

My tsconfig:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "noImplicitAny": false,
        "removeComments": true,
        "preserveConstEnums": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "outDir": "./dist"
    },
    "exclude": [
        "node_modules"
    ]
}
$ tsc --version
> message TS6029: Version 1.8.0-dev.20151108

Thanks!

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions