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

Do not emit files with no statments #10908

Closed
emilekberg opened this issue Sep 14, 2016 · 7 comments
Closed

Do not emit files with no statments #10908

emilekberg opened this issue Sep 14, 2016 · 7 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@emilekberg
Copy link

emilekberg commented Sep 14, 2016

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

@mhegazy
Copy link
Contributor

mhegazy commented Sep 14, 2016

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.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Sep 14, 2016
@emilekberg
Copy link
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.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Question An issue which isn't directly actionable in code labels Sep 14, 2016
@mhegazy mhegazy changed the title --outFile with system or amd emits empty module for interfaces Do not emit files with no statments Sep 14, 2016
@RyanCavanaugh
Copy link
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.

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Sep 28, 2016
@RyanCavanaugh
Copy link
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.

@MeirionHughes
Copy link

@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.

@kitsonk
Copy link
Contributor

kitsonk commented Aug 24, 2017

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.

@MeirionHughes
Copy link

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.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants