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

Imported libraries dropped when spread used on foo: any (new in 2.3) #15469

Closed
luggage66 opened this issue Apr 29, 2017 · 6 comments
Closed

Imported libraries dropped when spread used on foo: any (new in 2.3) #15469

luggage66 opened this issue Apr 29, 2017 · 6 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@luggage66
Copy link

luggage66 commented Apr 29, 2017

This is a new bug in 2.3. When you use a spread in JSX and the type of the variable being spread is "any" some referenced libraries are not kept.

There is also a full (but minimal) example in this repo: https://github.com/luggage66/ts23issue (just npm install && tsc && cat lib/foo.js )

TypeScript Version: 2.3.2

Code

import * as React from 'react';
import * as cx from 'classnames';

export default class FooComponent extends React.Component<any, void> {

    render() {
        let buttonProps; // any

        buttonProps = {
            onClick: () => undefined
        };

        return <button {...buttonProps}>
            <span className={cx('class1', { class2: true })} />
        </button>;
    }
}

Expected behavior:

The output to include require('classnames') (or the equivalent in your configured module system).

Actual behavior:

require('classnames') is missing when {...foo} is used when type of foo is "any".

@luggage66
Copy link
Author

Using --noImplicitAny will also cause require('classnames') to be in the output.

@diiq
Copy link

diiq commented Apr 30, 2017

I am also having dropped imports due to using the spread operator.

However, I have two differences from the reported issue:

  1. noImplicitAny doesn't seem to have an effect -- the imports are trimmed either way, for me.
  2. The object I am spreading is of type {[attributeName: string]: ''}, not any

The outcome is the same, though. With the spread, imports are trimmed for anything that is referenced only in the children of the jsx element. If I remove the spread, everything is okey-dokey; and if I roll back typescript to an earlier version, it's hunky-dory then, too.

Using es6-style modules fixes the problem, but only because it turns off import-trimming entirely :/.

@alitaheri
Copy link

casting to {} fixes it. it's a temporary fix until this is resolved.

@yuit yuit added the Fixed A PR has been merged for this issue label May 3, 2017
@yuit
Copy link
Contributor

yuit commented May 3, 2017

@diiq want to make sure I understand your scenario and that the PR address your issue
(Note i use above code with modify spread object type)

import * as React from 'react';
import * as cx from 'classnames';

export default class FooComponent extends React.Component<any, void> {

    render() {
        let buttonProps : {[attributeName: string]: ''}

        buttonProps = {
            onClick: () => undefined
        };

        return <button {...buttonProps}>
            <span className={cx('class1', { class2: true })} />
        </button>;
    }
}
import * as React from 'react';
export default class FooComponent extends React.Component {
    render() {
        // with this line, it'll include the 'classnames' lib
        // let buttonProps: object;
        // with this line, it will not
        let buttonProps; // any
        buttonProps = {
            onClick: () => undefined
        };
        //let i = <span className={cx('class1', { class2: true })} />;
        return React.createElement("button", Object.assign({}, buttonProps),
            React.createElement("span", { className: cx('class1', { class2: true }) }));
    }
}

@diiq
Copy link

diiq commented May 3, 2017

@yuti that looks right. This is used all over my codebase, so I rolled back to < 2.3 -- which means I haven't verified that what you posted makes the problem occur, but it does look like my situation.

I'm using React and Glamor; it's spreading Glamor's StyleAttribute type that causes the problem, if that helps.

Let me know if you need more info from me.

@yuit
Copy link
Contributor

yuit commented May 3, 2017

@diiq thanks for the information. The PR should address your issue as well. @luggage66 @alitaheri @diiq Give tomorrow nightly built a try and let us know

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants