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

Compiler generates code that uses an undefined variable when decorating properties that have nullable types #13955

Closed
nsnichols opened this issue Feb 8, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@nsnichols
Copy link

TypeScript Version: 2.1.5

Code

"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictNullChecks": true
}
// In file thing.ts
export class Thing {}
// In file thing-holder.ts

import {Thing} from "./thing";

function decorator() {
    return (...v: any[]) => {};
}
export class ThingHolder {
    @decorator()
    private thing: Thing | null;
}

The compiled javascript for ThingHolder looks as follows:

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function decorator() {
    return function () {
        var v = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            v[_i] = arguments[_i];
        }
    };
}
var ThingHolder = (function () {
    function ThingHolder() {
    }
    return ThingHolder;
}());
__decorate([
    decorator(),
    __metadata("design:type", thing_1.Thing)
], ThingHolder.prototype, "thing", void 0);
exports.ThingHolder = ThingHolder;

Notice the reference to thing_1 in the call to __decorate which should have been the result of a call to require("./thing").

If I change the type of thing in ThingHolder and remove the | null, the call to require("./thing") gets added as expected.

Expected behavior:

Thing gets imported

Actual behavior:

Thing is not imported

@mhegazy
Copy link
Contributor

mhegazy commented Feb 10, 2017

Duplicate of #13449

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 10, 2017
@mhegazy mhegazy closed this as completed Apr 21, 2017
@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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants