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

Typescript: ambient declaration file that declares a namespace results in build errors #7167

Closed
andyhopp opened this issue Jun 4, 2019 · 5 comments · Fixed by #7179
Closed

Comments

@andyhopp
Copy link
Contributor

andyhopp commented Jun 4, 2019

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes
$ npm --version
6.9.0

Which terms did you search for in User Guide?

Typescript Declaration @types

Environment

Environment Info:

System:
OS: macOS High Sierra 10.13.6
CPU: x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
Binaries:
Node: 10.15.3 - /usr/local/bin/node
npm: 6.9.0 - /usr/local/bin/npm
Browsers:
Chrome: 74.0.3729.169
Firefox: 60.6.2
Safari: 12.1.1
npmPackages:
react: ^16.8.6 => 16.8.6
react-dom: ^16.8.6 => 16.8.6
react-scripts: 3.0.1 => 3.0.1
npmGlobalPackages:
create-react-app: 2.1.8

Steps to Reproduce

  1. create-react-app my-app --typescript
  2. cd my-app
  3. npm i --save-dev @types/amazon-connect-streams
  4. Create a file named src/connect-service.ts:
export class Connect {
    constructor() {
    }

    public initialize(ccpElement: HTMLElement, options: connect.InitCCPOptions) {
        connect.core.initCCP(ccpElement, options);
        console.log('CCP Initialized!');
    }
}
  1. npm i -g typescript # To demonstrate the Typescript compiler
  2. tsc --build tsconfig.json # Note no errors
  3. npm run-script build

Expected Behavior

I expected the CRA build to behave similarly to the native TS compiler.

Actual Behavior

=============
Failed to compile.

./src/connect-service.ts
  Line 6:  'connect' is not defined  no-undef

Search for the keywords to learn more about each error.


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! connect-react-example@0.1.0 build: `react-scripts build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the connect-react-example@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Reproducible Demo

The steps above only require an NPM install of a types file and a single file to be created.

@robertvansteen
Copy link
Contributor

I don't know why there are no errors when running this through tsc but the error makes sense, connect is not defined. Why would you expect this to work in TypeScript?

@andyhopp
Copy link
Contributor Author

andyhopp commented Jun 6, 2019

Thanks for responding, @rovansteen! It's defined in the @types/amazon-connect-streams declarations file; the javascript library that this file is describing declares a global namespace (therefore the "declare namespace connect" in the .d.ts file). You don't see an explicit definition because it's an ambient module (i.e., you don't explicitly bring it in).

[Edit: I hereby retract my claim that babel-loader was at fault; it would appear that it's the linter. Researching further...]

@andyhopp
Copy link
Contributor Author

andyhopp commented Jun 6, 2019

It appears that the culprit is the typescript-eslint no-undef rule; there appears to be a bit of debate around this particular rule, with people pointing out that the typescript compiler catches this. Adding a line to eslint-config-react-app resolves the issue. I have also verified that the compiler catches undefined namespaces (simply changing the namespace to "connect0" results in the expected compiler error.).

@andyhopp
Copy link
Contributor Author

andyhopp commented Jun 6, 2019

@Timer Is there a way to find out whether this tweak would be included in the next release of CRA? (3.0.2, I suppose?)

@iansu
Copy link
Contributor

iansu commented Jun 6, 2019

Yes, it will be included in the 3.0.2 release.

@lock lock bot locked and limited conversation to collaborators Jun 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants