-
Notifications
You must be signed in to change notification settings - Fork 12.9k
import statement from "node" in js source file could produce correct declaration path. #41816
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/baselines/reference/importDeclFromTypeNodeInJsSource.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//// [tests/cases/compiler/importDeclFromTypeNodeInJsSource.ts] //// | ||
|
||
//// [index.d.ts] | ||
/// <reference path="events.d.ts" /> | ||
//// [events.d.ts] | ||
declare module "events" { | ||
namespace EventEmitter { | ||
class EventEmitter { | ||
constructor(); | ||
} | ||
} | ||
export = EventEmitter; | ||
} | ||
|
||
//// [b.js] | ||
import { EventEmitter } from 'events'; | ||
class Foo extends EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
export default Foo; | ||
|
||
//// [b.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const events_1 = require("events"); | ||
class Foo extends events_1.EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
exports.default = Foo; | ||
|
||
|
||
//// [b.d.ts] | ||
export default Foo; | ||
declare class Foo extends EventEmitter { | ||
} | ||
import { EventEmitter } from "events"; | ||
35 changes: 35 additions & 0 deletions
35
tests/baselines/reference/importDeclFromTypeNodeInJsSource.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
=== /src/node_modules/@types/node/index.d.ts === | ||
/// <reference path="events.d.ts" /> | ||
No type information for this code.=== /src/node_modules/@types/node/events.d.ts === | ||
declare module "events" { | ||
>"events" : Symbol("events", Decl(events.d.ts, 0, 0)) | ||
|
||
namespace EventEmitter { | ||
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 0, 25)) | ||
|
||
class EventEmitter { | ||
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 1, 28)) | ||
|
||
constructor(); | ||
} | ||
} | ||
export = EventEmitter; | ||
>EventEmitter : Symbol(EventEmitter, Decl(events.d.ts, 0, 25)) | ||
} | ||
|
||
=== /src/b.js === | ||
import { EventEmitter } from 'events'; | ||
>EventEmitter : Symbol(EventEmitter, Decl(b.js, 0, 8)) | ||
|
||
class Foo extends EventEmitter { | ||
>Foo : Symbol(Foo, Decl(b.js, 0, 38)) | ||
>EventEmitter : Symbol(EventEmitter, Decl(b.js, 0, 8)) | ||
|
||
constructor() { | ||
super(); | ||
>super : Symbol(EventEmitter, Decl(events.d.ts, 1, 28)) | ||
} | ||
} | ||
export default Foo; | ||
>Foo : Symbol(Foo, Decl(b.js, 0, 38)) | ||
|
36 changes: 36 additions & 0 deletions
36
tests/baselines/reference/importDeclFromTypeNodeInJsSource.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
=== /src/node_modules/@types/node/index.d.ts === | ||
/// <reference path="events.d.ts" /> | ||
No type information for this code.=== /src/node_modules/@types/node/events.d.ts === | ||
declare module "events" { | ||
>"events" : typeof import("events") | ||
|
||
namespace EventEmitter { | ||
>EventEmitter : typeof EventEmitter | ||
|
||
class EventEmitter { | ||
>EventEmitter : EventEmitter | ||
|
||
constructor(); | ||
} | ||
} | ||
export = EventEmitter; | ||
>EventEmitter : typeof EventEmitter | ||
} | ||
|
||
=== /src/b.js === | ||
import { EventEmitter } from 'events'; | ||
>EventEmitter : typeof EventEmitter | ||
|
||
class Foo extends EventEmitter { | ||
>Foo : Foo | ||
>EventEmitter : EventEmitter | ||
|
||
constructor() { | ||
super(); | ||
>super() : void | ||
>super : typeof EventEmitter | ||
} | ||
} | ||
export default Foo; | ||
>Foo : Foo | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// @target: esnext | ||
// @module: commonjs | ||
// @allowJs: true | ||
// @checkJs: true | ||
// @declaration: true | ||
// @outDir: ./dist | ||
// @filename: /src/node_modules/@types/node/index.d.ts | ||
/// <reference path="events.d.ts" /> | ||
// @filename: /src/node_modules/@types/node/events.d.ts | ||
declare module "events" { | ||
namespace EventEmitter { | ||
class EventEmitter { | ||
constructor(); | ||
} | ||
} | ||
export = EventEmitter; | ||
} | ||
|
||
// @filename: /src/b.js | ||
import { EventEmitter } from 'events'; | ||
class Foo extends EventEmitter { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
export default Foo; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so the key point to this test is that this
.d.ts
doesn't turn intonode/events
because it's a special case global 👍🏻Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix seems incorrect and an odd special case. We expect in cases like this one, to use the module name specified by the ambient module declaration, not one from a filepath. Since the module is declared as
declare module "events"
we should only be using"events"
to refer to it, and should never have looked at a filepath to begin with (notably: there's no guarantee the ambient module has the same name as the containing file!).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change needs to be reverted and correctly fixed as @weswigham points out correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh.... Sorry for the misleading...
And, I am a little confused, if there is not any special folder, according to the module resolution https://www.typescriptlang.org/docs/handbook/module-resolution.html. How could file find the correct modules? Did I miss something?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Auh, I find this https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html#-reference-ing-a-module and finally understand why it is called "ambient" module......
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#41895 this one!