Skip to content

Fix merging of JS value & TS type decl #38936

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 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7870,7 +7870,7 @@ namespace ts {
(resolvedSymbol || symbol).exports!.forEach((s, name) => {
const exportedMember = members.get(name)!;
if (exportedMember && exportedMember !== s) {
if (s.flags & SymbolFlags.Value) {
if (s.flags & SymbolFlags.Value && exportedMember.flags & SymbolFlags.Value) {
Copy link
Member

Choose a reason for hiding this comment

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

@weswigham @sandersn when I was looking at this with Eli, this brought up a question as to why/how exportedType (defined a few lines above—the type of require('./y.js') in the test case) could have non-value members. In resolveAnonymousTypeMembers, it copies the exports of the symbol to the type, but of course, the exports of the .d.ts module includes a type symbol. It was surprising to me that resolveStructuredTypeMembers can return an anonymous object type that has members that are also types. Is that normal?

Copy link
Member

Choose a reason for hiding this comment

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

normal-ish -- it can easily happen when you merge a type-only namespace with an object literal:

var o = { x: 1 }
namespace o { export type y = number }

Copy link
Member

Choose a reason for hiding this comment

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

I realized I was confusing anonymous type members with anonymous type properties, which are guaranteed to be values. With that confusion cleared up, this fix definitely looks correct. The possibility of type members was simply overlooked in the past.

// If the member has an additional value-like declaration, union the types from the two declarations,
// but issue an error if they occurred in two different files. The purpose is to support a JS file with
// a pattern like:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== /x.js ===
module.exports.x = 1;
>module.exports.x : Symbol(x, Decl(x.js, 0, 0), Decl(y.d.ts, 0, 0))
>module.exports : Symbol(x, Decl(x.js, 0, 0), Decl(y.d.ts, 0, 0))
>module : Symbol(module, Decl(x.js, 0, 0))
>exports : Symbol("/x", Decl(x.js, 0, 0))
>x : Symbol(x, Decl(x.js, 0, 0), Decl(y.d.ts, 0, 0))

module.exports = require("./y.js");
>module.exports : Symbol("/x", Decl(x.js, 0, 0))
>module : Symbol(export=, Decl(x.js, 0, 21))
>exports : Symbol(export=, Decl(x.js, 0, 21))
>require : Symbol(require)
>"./y.js" : Symbol("/y", Decl(y.d.ts, 0, 0))

=== /y.d.ts ===
export declare type x = 1;
>x : Symbol(x, Decl(x.js, 0, 0), Decl(y.d.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /x.js ===
module.exports.x = 1;
>module.exports.x = 1 : 1
>module.exports.x : number
>module.exports : typeof import("/y")
>module : { "\"/x\"": typeof import("/y"); }
>exports : typeof import("/y")
>x : number
>1 : 1

module.exports = require("./y.js");
>module.exports = require("./y.js") : typeof import("/y")
>module.exports : typeof import("/y")
>module : { "\"/x\"": typeof import("/y"); }
>exports : typeof import("/y")
>require("./y.js") : typeof import("/y")
>require : any
>"./y.js" : "./y.js"

=== /y.d.ts ===
export declare type x = 1;
>x : 1

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true

// @Filename: /x.js
module.exports.x = 1;
module.exports = require("./y.js");

// @Filename: /y.d.ts
export declare type x = 1;