Fix js declaration emit for exporting default which looks like namespace merge (#36482)

Fixes #35074
This commit is contained in:
Sheetal Nandi
2020-01-28 12:21:36 -08:00
committed by GitHub
parent 2cc7a5d6bf
commit f91f762ced
5 changed files with 150 additions and 2 deletions
+2 -2
View File
@@ -3378,7 +3378,7 @@ namespace ts {
// `sym` may not have exports if this module declaration is backed by the symbol for a `const` that's being rewritten
// into a namespace - in such cases, it's best to just let the namespace appear empty (the const members couldn't have referred
// to one another anyway)
if (result = callback(sym.exports || emptySymbols)) {
if (result = callback(sym?.exports || emptySymbols)) {
return result;
}
break;
@@ -6134,7 +6134,7 @@ namespace ts {
}
function getDeclarationWithTypeAnnotation(symbol: Symbol) {
return find(symbol.declarations, s => !!getEffectiveTypeAnnotationNode(s) && !!findAncestor(s, n => n === enclosingDeclaration));
return symbol.declarations && find(symbol.declarations, s => !!getEffectiveTypeAnnotationNode(s) && !!findAncestor(s, n => n === enclosingDeclaration));
}
/**
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/jsDeclarationsWithDefaultAsNamespaceLikeMerge.ts] ////
//// [helper.d.ts]
type Computed = () => any;
interface Mapper<R> {
<Key extends string>(map: Key[]): { [K in Key]: R };
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
}
interface NamespacedMappers {
mapState: Mapper<Computed>;
}
export declare function createNamespacedHelpers(): NamespacedMappers;
//// [index.js]
import { createNamespacedHelpers } from './helper'
const { mapState } = createNamespacedHelpers()
export default {
computed: {
...mapState(['panels'])
}
}
//// [index.d.ts]
declare namespace _default {
export namespace computed {
export const panels: import("./helper").Computed;
}
}
export default _default;
@@ -0,0 +1,53 @@
=== /helper.d.ts ===
type Computed = () => any;
>Computed : Symbol(Computed, Decl(helper.d.ts, 0, 0))
interface Mapper<R> {
>Mapper : Symbol(Mapper, Decl(helper.d.ts, 0, 26))
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
<Key extends string>(map: Key[]): { [K in Key]: R };
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
>map : Symbol(map, Decl(helper.d.ts, 2, 25))
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
>K : Symbol(K, Decl(helper.d.ts, 2, 41))
>Key : Symbol(Key, Decl(helper.d.ts, 2, 5))
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
>map : Symbol(map, Decl(helper.d.ts, 3, 41))
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
>K : Symbol(K, Decl(helper.d.ts, 3, 55))
>Map : Symbol(Map, Decl(helper.d.ts, 3, 5))
>R : Symbol(R, Decl(helper.d.ts, 1, 17))
}
interface NamespacedMappers {
>NamespacedMappers : Symbol(NamespacedMappers, Decl(helper.d.ts, 4, 1))
mapState: Mapper<Computed>;
>mapState : Symbol(NamespacedMappers.mapState, Decl(helper.d.ts, 5, 29))
>Mapper : Symbol(Mapper, Decl(helper.d.ts, 0, 26))
>Computed : Symbol(Computed, Decl(helper.d.ts, 0, 0))
}
export declare function createNamespacedHelpers(): NamespacedMappers;
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(helper.d.ts, 7, 1))
>NamespacedMappers : Symbol(NamespacedMappers, Decl(helper.d.ts, 4, 1))
=== /index.js ===
import { createNamespacedHelpers } from './helper'
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(index.js, 0, 8))
const { mapState } = createNamespacedHelpers()
>mapState : Symbol(mapState, Decl(index.js, 1, 7))
>createNamespacedHelpers : Symbol(createNamespacedHelpers, Decl(index.js, 0, 8))
export default {
computed: {
>computed : Symbol(computed, Decl(index.js, 2, 16))
...mapState(['panels'])
>mapState : Symbol(mapState, Decl(index.js, 1, 7))
}
}
@@ -0,0 +1,41 @@
=== /helper.d.ts ===
type Computed = () => any;
>Computed : Computed
interface Mapper<R> {
<Key extends string>(map: Key[]): { [K in Key]: R };
>map : Key[]
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
>map : Map
}
interface NamespacedMappers {
mapState: Mapper<Computed>;
>mapState : Mapper<Computed>
}
export declare function createNamespacedHelpers(): NamespacedMappers;
>createNamespacedHelpers : () => NamespacedMappers
=== /index.js ===
import { createNamespacedHelpers } from './helper'
>createNamespacedHelpers : () => import("/helper").NamespacedMappers
const { mapState } = createNamespacedHelpers()
>mapState : import("/helper").Mapper<import("/helper").Computed>
>createNamespacedHelpers() : import("/helper").NamespacedMappers
>createNamespacedHelpers : () => import("/helper").NamespacedMappers
export default {
>{ computed: { ...mapState(['panels']) }} : { computed: { panels: import("/helper").Computed; }; }
computed: {
>computed : { panels: import("/helper").Computed; }
>{ ...mapState(['panels']) } : { panels: import("/helper").Computed; }
...mapState(['panels'])
>mapState(['panels']) : { panels: import("/helper").Computed; }
>mapState : import("/helper").Mapper<import("/helper").Computed>
>['panels'] : "panels"[]
>'panels' : "panels"
}
}
@@ -0,0 +1,23 @@
// @declaration: true
// @allowJs: true
// @emitDeclarationOnly: true
// @filename: /helper.d.ts
type Computed = () => any;
interface Mapper<R> {
<Key extends string>(map: Key[]): { [K in Key]: R };
<Map extends Record<string, string>>(map: Map): { [K in keyof Map]: R };
}
interface NamespacedMappers {
mapState: Mapper<Computed>;
}
export declare function createNamespacedHelpers(): NamespacedMappers;
// @filename: /index.js
import { createNamespacedHelpers } from './helper'
const { mapState } = createNamespacedHelpers()
export default {
computed: {
...mapState(['panels'])
}
}