Skip to content

Commit f91f762

Browse files
authored
Fix js declaration emit for exporting default which looks like namespace merge (#36482)
Fixes #35074
1 parent 2cc7a5d commit f91f762

File tree

5 files changed

+150
-2
lines changed

5 files changed

+150
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ namespace ts {
33783378
// `sym` may not have exports if this module declaration is backed by the symbol for a `const` that's being rewritten
33793379
// into a namespace - in such cases, it's best to just let the namespace appear empty (the const members couldn't have referred
33803380
// to one another anyway)
3381-
if (result = callback(sym.exports || emptySymbols)) {
3381+
if (result = callback(sym?.exports || emptySymbols)) {
33823382
return result;
33833383
}
33843384
break;
@@ -6134,7 +6134,7 @@ namespace ts {
61346134
}
61356135

61366136
function getDeclarationWithTypeAnnotation(symbol: Symbol) {
6137-
return find(symbol.declarations, s => !!getEffectiveTypeAnnotationNode(s) && !!findAncestor(s, n => n === enclosingDeclaration));
6137+
return symbol.declarations && find(symbol.declarations, s => !!getEffectiveTypeAnnotationNode(s) && !!findAncestor(s, n => n === enclosingDeclaration));
61386138
}
61396139

61406140
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/jsDeclarationsWithDefaultAsNamespaceLikeMerge.ts] ////
2+
3+
//// [helper.d.ts]
4+
type Computed = () => any;
5+
interface Mapper<R> {
6+
<Key extends string>(map: Key[]): { [K in Key]: R };
7+
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
8+
}
9+
interface NamespacedMappers {
10+
mapState: Mapper<Computed>;
11+
}
12+
export declare function createNamespacedHelpers(): NamespacedMappers;
13+
14+
//// [index.js]
15+
import { createNamespacedHelpers } from './helper'
16+
const { mapState } = createNamespacedHelpers()
17+
export default {
18+
computed: {
19+
...mapState(['panels'])
20+
}
21+
}
22+
23+
24+
25+
//// [index.d.ts]
26+
declare namespace _default {
27+
export namespace computed {
28+
export const panels: import("./helper").Computed;
29+
}
30+
}
31+
export default _default;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
=== /helper.d.ts ===
2+
type Computed = () => any;
3+
>Computed : Symbol(Computed, Decl(helper.d.ts, 0, 0))
4+
5+
interface Mapper<R> {
6+
>Mapper : Symbol(Mapper, Decl(helper.d.ts, 0, 26))
7+
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
8+
9+
<Key extends string>(map: Key[]): { [K in Key]: R };
10+
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
11+
>map : Symbol(map, Decl(helper.d.ts, 2, 25))
12+
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
13+
>K : Symbol(K, Decl(helper.d.ts, 2, 41))
14+
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
15+
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
16+
17+
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
18+
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
19+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
20+
>map : Symbol(map, Decl(helper.d.ts, 3, 41))
21+
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
22+
>K : Symbol(K, Decl(helper.d.ts, 3, 55))
23+
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
24+
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
25+
}
26+
interface NamespacedMappers {
27+
>NamespacedMappers : Symbol(NamespacedMappers, Decl(helper.d.ts, 4, 1))
28+
29+
mapState: Mapper<Computed>;
30+
>mapState : Symbol(NamespacedMappers.mapState, Decl(helper.d.ts, 5, 29))
31+
>Mapper : Symbol(Mapper, Decl(helper.d.ts, 0, 26))
32+
>Computed : Symbol(Computed, Decl(helper.d.ts, 0, 0))
33+
}
34+
export declare function createNamespacedHelpers(): NamespacedMappers;
35+
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(helper.d.ts, 7, 1))
36+
>NamespacedMappers : Symbol(NamespacedMappers, Decl(helper.d.ts, 4, 1))
37+
38+
=== /index.js ===
39+
import { createNamespacedHelpers } from './helper'
40+
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(index.js, 0, 8))
41+
42+
const { mapState } = createNamespacedHelpers()
43+
>mapState : Symbol(mapState, Decl(index.js, 1, 7))
44+
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(index.js, 0, 8))
45+
46+
export default {
47+
computed: {
48+
>computed : Symbol(computed, Decl(index.js, 2, 16))
49+
50+
...mapState(['panels'])
51+
>mapState : Symbol(mapState, Decl(index.js, 1, 7))
52+
}
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== /helper.d.ts ===
2+
type Computed = () => any;
3+
>Computed : Computed
4+
5+
interface Mapper<R> {
6+
<Key extends string>(map: Key[]): { [K in Key]: R };
7+
>map : Key[]
8+
9+
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
10+
>map : Map
11+
}
12+
interface NamespacedMappers {
13+
mapState: Mapper<Computed>;
14+
>mapState : Mapper<Computed>
15+
}
16+
export declare function createNamespacedHelpers(): NamespacedMappers;
17+
>createNamespacedHelpers : () => NamespacedMappers
18+
19+
=== /index.js ===
20+
import { createNamespacedHelpers } from './helper'
21+
>createNamespacedHelpers : () => import("/helper").NamespacedMappers
22+
23+
const { mapState } = createNamespacedHelpers()
24+
>mapState : import("/helper").Mapper<import("/helper").Computed>
25+
>createNamespacedHelpers() : import("/helper").NamespacedMappers
26+
>createNamespacedHelpers : () => import("/helper").NamespacedMappers
27+
28+
export default {
29+
>{ computed: { ...mapState(['panels']) }} : { computed: { panels: import("/helper").Computed; }; }
30+
31+
computed: {
32+
>computed : { panels: import("/helper").Computed; }
33+
>{ ...mapState(['panels']) } : { panels: import("/helper").Computed; }
34+
35+
...mapState(['panels'])
36+
>mapState(['panels']) : { panels: import("/helper").Computed; }
37+
>mapState : import("/helper").Mapper<import("/helper").Computed>
38+
>['panels'] : "panels"[]
39+
>'panels' : "panels"
40+
}
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @declaration: true
2+
// @allowJs: true
3+
// @emitDeclarationOnly: true
4+
5+
// @filename: /helper.d.ts
6+
type Computed = () => any;
7+
interface Mapper<R> {
8+
<Key extends string>(map: Key[]): { [K in Key]: R };
9+
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
10+
}
11+
interface NamespacedMappers {
12+
mapState: Mapper<Computed>;
13+
}
14+
export declare function createNamespacedHelpers(): NamespacedMappers;
15+
16+
// @filename: /index.js
17+
import { createNamespacedHelpers } from './helper'
18+
const { mapState } = createNamespacedHelpers()
19+
export default {
20+
computed: {
21+
...mapState(['panels'])
22+
}
23+
}

0 commit comments

Comments
 (0)