Skip to content

Commit 6714b2b

Browse files
committed
Do not do this in JS files, rationale in comment
1 parent 70dba24 commit 6714b2b

7 files changed

+9
-75
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5836,9 +5836,11 @@ namespace ts {
58365836
//
58375837
// We check that this signature's declaration is within the existing enclosing declaration so we don't pull
58385838
// in types from other files; if it is outside of the enclosing declaration, then we shouldn't be able to
5839-
// access those parameter types via typeof anyway.
5839+
// access those parameter types via typeof anyway. The declaration should also not be in a JS file; those can
5840+
// only be typed via JSDoc, but JSDoc annotations appear outside of the function and therefore are not in
5841+
// the function's scope, unlike true type annotations.
58405842
const saveEnclosingDeclaration = context.enclosingDeclaration;
5841-
if (signature.declaration && saveEnclosingDeclaration && findAncestor(signature.declaration, (n) => n === saveEnclosingDeclaration)) {
5843+
if (signature.declaration && !isInJSFile(signature.declaration) && saveEnclosingDeclaration && findAncestor(signature.declaration, (n) => n === saveEnclosingDeclaration)) {
58425844
context.enclosingDeclaration = signature.declaration;
58435845
}
58445846

tests/baselines/reference/jsDeclarationsFunctions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ declare class Cls {
164164
export function g(a: {
165165
x: string;
166166
}, b: {
167-
y: typeof import(".").b;
167+
y: typeof b;
168168
}): void;
169169
/**
170170
* @param {{x: string}} a
@@ -173,6 +173,6 @@ export function g(a: {
173173
declare function hh(a: {
174174
x: string;
175175
}, b: {
176-
y: typeof import(".").b;
176+
y: typeof b;
177177
}): void;
178178
export { hh as h, i as ii, j as jj };

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespace.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,3 @@ import { myTypes } from "./file.js";
9292
export const testFnTypes: {
9393
[x: string]: any;
9494
};
95-
export namespace testFnTypes {
96-
type input = boolean | myTypes.typeC;
97-
}

tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespaceCjs.js

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -96,69 +96,4 @@ export const testFnTypes: {
9696
export namespace testFnTypes {
9797
type input = boolean | myTypes.typeC;
9898
}
99-
export namespace testFnTypes {
100-
type input = boolean | myTypes.typeC;
101-
}
10299
import { myTypes } from "./file.js";
103-
104-
105-
//// [DtsFileErrors]
106-
107-
108-
tests/cases/conformance/jsdoc/declarations/file2.d.ts(18,10): error TS2300: Duplicate identifier 'input'.
109-
tests/cases/conformance/jsdoc/declarations/file2.d.ts(21,10): error TS2300: Duplicate identifier 'input'.
110-
111-
112-
==== tests/cases/conformance/jsdoc/declarations/file2.d.ts (2 errors) ====
113-
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */
114-
/**
115-
* @function testFn
116-
* @description A test function.
117-
* @param {testFnTypes.input} input - Input.
118-
* @returns {number|null} Result.
119-
*/
120-
export function testFn(input: testFnTypes.input): number | null;
121-
/**
122-
* @namespace testFnTypes
123-
* @global
124-
* @type {Object<string,*>}
125-
*/
126-
export const testFnTypes: {
127-
[x: string]: any;
128-
};
129-
export namespace testFnTypes {
130-
type input = boolean | myTypes.typeC;
131-
~~~~~
132-
!!! error TS2300: Duplicate identifier 'input'.
133-
}
134-
export namespace testFnTypes {
135-
type input = boolean | myTypes.typeC;
136-
~~~~~
137-
!!! error TS2300: Duplicate identifier 'input'.
138-
}
139-
import { myTypes } from "./file.js";
140-
141-
==== /.src/tests/cases/conformance/jsdoc/declarations/file.d.ts (0 errors) ====
142-
/**
143-
* @namespace myTypes
144-
* @global
145-
* @type {Object<string,*>}
146-
*/
147-
export const myTypes: {
148-
[x: string]: any;
149-
};
150-
export namespace myTypes {
151-
type typeA = string | RegExp | Array<string | RegExp>;
152-
type typeB = {
153-
/**
154-
* - Prop 1.
155-
*/
156-
prop1: myTypes.typeA;
157-
/**
158-
* - Prop 2.
159-
*/
160-
prop2: string;
161-
};
162-
type typeC = myTypes.typeB | Function;
163-
}
164-

tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ declare const couldntThinkOfAny: {};
8686
* @param {InstanceType<BaseFactory["Base"]>} base
8787
* @returns {InstanceType<BaseFactory["Base"]>}
8888
*/
89-
declare function test(base: InstanceType<BaseFactory["Base"]>): {};
89+
declare function test(base: InstanceType<BaseFactory["Base"]>): InstanceType<BaseFactory["Base"]>;
9090
type BaseFactory = {
9191
(): {};
9292
Base: {

tests/baselines/reference/jsDeclarationsParameterTagReusesInputNodeInEmit2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ declare class Base {
6262
* @param {InstanceType<BaseFactory["Base"]>} base
6363
* @returns {InstanceType<BaseFactory["Base"]>}
6464
*/
65-
declare function test(base: InstanceType<BaseFactory["Base"]>): {};
65+
declare function test(base: InstanceType<BaseFactory["Base"]>): InstanceType<BaseFactory["Base"]>;
6666
type BaseFactory = typeof import('./base');

tests/baselines/reference/typeTagOnPropertyAssignment.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const o = {
1313
/** @type {() => 'b'} */
1414
n: () => 'b'
1515
>n : () => 'b'
16-
>() => 'b' : () => "b"
16+
>() => 'b' : () => 'b'
1717
>'b' : "b"
1818

1919
};

0 commit comments

Comments
 (0)