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

Add code fix to convert 'require' in a '.ts' file to an 'import' #23711

Merged
3 commits merged into from
May 1, 2018

Conversation

ghost
Copy link

@ghost ghost commented Apr 26, 2018

In TypeScript code, a require() call gives you any even in --noImplicitAny mode. Top-level requires can be converted to import = statements, or default imports if --allowSyntheticDefaultImports is enabled.

@ghost ghost requested review from DanielRosenwasser, mhegazy and amcasey April 26, 2018 16:07
@mhegazy
Copy link
Contributor

mhegazy commented Apr 26, 2018

sometimes you do that to avoid making your file a module.. we should check that the file is already a module before offering this suggestion, otherwise more errors would be produced.

@@ -32,6 +32,19 @@ namespace ts {
}
check(sourceFile);

if (!isJsFile && sourceFile.externalModuleIndicator) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i take back my original comment. we should just do this all the time. with this check enabled a simple scenario like #23780 would not trigger the suggestion..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about flip-flopping on this issue.

@WangPengJu
Copy link

WangPengJu commented May 1, 2018

just like you say "sometimes you do that to avoid making your file a module". @mhegazy
a.ts

import fs = require("fs");
namespace G {
    export let a = 0;
}

b.ts

G.a // error 

I want use namespace as global. not single module. but if use "import fs = require('fs')" in "a.ts", "b.ts" can't use G. if use "const fs = require("fs")", it's work.

"const fs = require("fs")" no intellisense in vscode, webstorm have intellisense. it's confuse me.

@ghost ghost merged commit cee4289 into master May 1, 2018
@ghost ghost deleted the requireInTs branch May 1, 2018 16:24
@ghost
Copy link
Author

ghost commented May 1, 2018

@WangPengJu require is just a function any => any -- webstorm must have a separate layer on top of TypeScript. I don't think there's any way to create a global namespace inside a module scope -- even in a commonjs system, a var in a module is still local, and that's what a namespace compiles to.

@WangPengJu
Copy link

WangPengJu commented May 2, 2018

finally, i will merger all file in one file(as global). so that is why i use namespace. not module. @andy-ms

@WangPengJu
Copy link

const fs: typeof import ("fs") = require("fs");

it's work.
use typescript: 2.9.0-dev.20180501

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This pull request was closed.
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 this pull request may close these issues.

2 participants