Skip to content

TypeScript 2.3 does not import static member from classes #15721

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

Closed
jquintozamora opened this issue May 10, 2017 · 2 comments
Closed

TypeScript 2.3 does not import static member from classes #15721

jquintozamora opened this issue May 10, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@jquintozamora
Copy link

jquintozamora commented May 10, 2017

TypeScript Version: 2.3.1 and 2.3.2

Actual behavior:
I did update my project from TypeScript 2.2 to 2.3.1 (and 2.3.2) and I observed some missed imports from static members of classes.

Code
This is how I create and export the static members:

export class ViewerItemCardType {
    public static Big: ViewerItemCardType = new ViewerItemCardType(1, "big", 330, 660);
    public static Small: ViewerItemCardType = new ViewerItemCardType(3, "small", 100, 200);
    private constructor(
        public id: number,
        public name: string,
        public imageHeight: number,
        public imageWidth: number
    ) { }
}

Which is converted using TSC into:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ViewerItemCardType = (function () {
    function ViewerItemCardType(id, name, imageHeight, imageWidth) {
        this.id = id;
        this.name = name;
        this.imageHeight = imageHeight;
        this.imageWidth = imageWidth;
    }
    return ViewerItemCardType;
}());
ViewerItemCardType.Big = new ViewerItemCardType(1, "big", 330, 660);
ViewerItemCardType.Small = new ViewerItemCardType(3, "small", 100, 200);
exports.ViewerItemCardType = ViewerItemCardType;

Here is how I import and use the static members:

import * as React from "react";
import ViewerItem from "../ViewerItem/ViewerItem";

import {ViewerItemCardType} from "../ViewerItem/ViewerItemCardType";

import { IViewerProps } from "./IViewerProps";
class Viewer extends React.Component<IViewerProps, {}> {
    public render() {
        const { id, article } = this.props;
        return (
            <ViewerItem {...article} typeSingleton={ViewerItemCardType.Big} />
        );
    }
}
export default Viewer;

Here is the code generated by TypeScript (which actually is missing the import).
Note that the generated code is trying to call ViewerItemCardType_1.ViewerItemCardType.Big but the variable ViewerItemCardType_1 wasn't actually imported properly.

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
        s = arguments[i];
        for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
            t[p] = s[p];
    }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var ViewerItem_1 = require("../ViewerItem/ViewerItem");
var Viewer = (function (_super) {
    __extends(Viewer, _super);
    function Viewer() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    Viewer.prototype.render = function () {
        var _a = this.props, id = _a.id, article = _a.article;
        return (React.createElement(ViewerItem_1.default, __assign({}, article, { typeSingleton: ViewerItemCardType_1.ViewerItemCardType.Big })));
    };
    return Viewer;
}(React.Component));
exports.default = Viewer;

My tsconfig:

{
    "compilerOptions": {
        "target": "es5",                          
        "module": "commonjs",                     
        "jsx": "react",                          
        "declaration": false,                   
        "sourceMap": true,                        
        "strict": true,                          
        "typeRoots": [
            "./node_modules/@types"
        ],                                        
        "types": [
            "node",
            "react",
            "react-dom"
        ]                                        
    }
}

Expected behavior:
Create the variable ViewerItemCardType_1 in the generated js file. It was working fine in TypeScript 2.2.

Thanks!

@mhegazy
Copy link
Contributor

mhegazy commented May 10, 2017

Looks like a duplicate of #15469. should be fixed in typescript@next and in the next TS 2.3.3 release next week.

can you give typescript@next a try and see if you are still running into issues?

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Bug A bug in TypeScript labels May 10, 2017
@jquintozamora
Copy link
Author

It worked for me using version 2.4.0-dev.20170511

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 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

3 participants