Skip to content

Do not emit files with no statments #10908

Closed

Description

TypeScript Version: 2.0.2

Code

//tsconfig.json
{
    "compilerOptions": {
        "module": "amd",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "outFile": "out.js"
    }
}
//in person.ts
export interface Person {
    name:string,
    age:number
}
//in student.ts
import {Person} from './person'
export class Student implements Person {
    name:string;
    age:number;
    constructor(name:string, age:number) {
        this.name = name;
        this.age = age;
    }
}

Expected behavior:

define("student", ["require", "exports"], function (require, exports) {
    "use strict";
    var Student = (function () {
        function Student(name, age) {
            this.name = name;
            this.age = age;
        }
        return Student;
    }());
    exports.Student = Student;
});

Actual behavior:

define("person", ["require", "exports"], function (require, exports) {
    "use strict";
});
define("student", ["require", "exports"], function (require, exports) {
    "use strict";
    var Student = (function () {
        function Student(name, age) {
            this.name = name;
            this.age = age;
        }
        return Student;
    }());
    exports.Student = Student;
});

note: adding "noImplicitUseStrict": true provides the same result, but without usestrict statement

Activity

  1. mhegazy commented on Sep 14, 2016

    @mhegazy
    Contributor

    Well.. there is a file called person.ts that happens to be a module. so a corresponding module is created for it. if person is just a set of declarations, change it is name to person.d.ts.

  2. emilekberg commented on Sep 14, 2016

    @emilekberg
    Author

    Sure, that might work, but if i use declaration: true, it causes some unintended behaviour, as the d.ts file is never built, it's not available in the bunded declaration file.
    This should be the also be the case if not using outFile, since it won't emit a d.ts from a d.ts. If a outDir is used, there will be no relative path from Person to Student.

    It seems quite weird to me that tsc would emit empty files if no code is generated from it.

  3. added
    SuggestionAn idea for TypeScript
    and removed
    QuestionAn issue which isn't directly actionable in code
    on Sep 14, 2016
  4. changed the title [-]--outFile with system or amd emits empty module for interfaces[/-] [+]Do not emit files with no statments[/+] on Sep 14, 2016
  5. RyanCavanaugh commented on Sep 16, 2016

    @RyanCavanaugh
    Member

    The problem is what happens when you go from a 1-statement file to a 0-statement file during separate compilations -- without emitting the blank file, you might still be loading and executing that 1 statement.

  6. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    SuggestionAn idea for TypeScript
    on Sep 28, 2016
  7. RyanCavanaugh commented on Sep 28, 2016

    @RyanCavanaugh
    Member

    The empty module still has side effects by its mere existence that you may want to occur. Renaming to .d.ts and piping it through your build manually is the intended workaround if you want a types-only module to not be manifest despite being imported.

  8. MeirionHughes commented on Aug 24, 2017

    @MeirionHughes

    Ryan Cavanaugh (@RyanCavanaugh) using .d.ts works locally, but the .d.ts files don't get compiled/copied out to the dist during build - which I'd need for the library. If I keep everything as .ts files, then the type/interface only files compile out to empty ts files + definition.

  9. kitsonk commented on Aug 24, 2017

    @kitsonk
    Contributor

    but the .d.ts files don't get compiled/copied out to the dist during build

    $ npm install copy -g
    $ copy ./src/**/*.d.ts ./dist

    From the TypeScript Non-Goals:

    1. Provide an end-to-end build pipeline. Instead, make the system extensible so that external tools can use the compiler for more complex build workflows.
  10. MeirionHughes commented on Aug 24, 2017

    @MeirionHughes

    Sure, I know that; but can't there be a emitEmpty: boolean in the compileOptions?
    I'd pick that up if TypeScript guys would accept community pr for it.

  11. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions