-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add error when importing/exporting types in JS files #49580
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
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
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
34 changes: 34 additions & 0 deletions
34
tests/baselines/reference/importingExportingTypes.errors.txt
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,34 @@ | ||
/index.js(1,21): error TS18042: 'WriteFileOptions' is a type and cannot be imported in JavaScript files. Use 'import("fs").WriteFileOptions' in a JSDoc type annotation. | ||
/index.js(1,39): error TS18042: 'WriteFileOptions' is a type and cannot be imported in JavaScript files. Use 'import("fs").WriteFileOptions' in a JSDoc type annotation. | ||
/index.js(5,10): error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
/index.js(6,10): error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
/index.js(7,10): error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
|
||
|
||
==== /node_modules/@types/node/index.d.ts (0 errors) ==== | ||
declare module "fs" { | ||
export interface WriteFileOptions {} | ||
export function writeFile(path: string, data: any, options: WriteFileOptions, callback: (err: Error) => void): void; | ||
} | ||
|
||
==== /index.js (5 errors) ==== | ||
import { writeFile, WriteFileOptions, WriteFileOptions as OtherName } from "fs"; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS18042: 'WriteFileOptions' is a type and cannot be imported in JavaScript files. Use 'import("fs").WriteFileOptions' in a JSDoc type annotation. | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS18042: 'WriteFileOptions' is a type and cannot be imported in JavaScript files. Use 'import("fs").WriteFileOptions' in a JSDoc type annotation. | ||
|
||
/** @typedef {{ x: any }} JSDocType */ | ||
|
||
export { JSDocType }; | ||
~~~~~~~~~ | ||
!!! error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
!!! related TS18044 /index.js:3:5: 'JSDocType' is automatically exported here. | ||
export { JSDocType as ThisIsFine }; | ||
~~~~~~~~~ | ||
!!! error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
!!! related TS18044 /index.js:3:5: 'JSDocType' is automatically exported here. | ||
export { WriteFileOptions }; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS18043: Types cannot appear in export declarations in JavaScript files. | ||
|
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,37 @@ | ||
=== /node_modules/@types/node/index.d.ts === | ||
declare module "fs" { | ||
>"fs" : Symbol("fs", Decl(index.d.ts, 0, 0)) | ||
|
||
export interface WriteFileOptions {} | ||
>WriteFileOptions : Symbol(WriteFileOptions, Decl(index.d.ts, 0, 21)) | ||
|
||
export function writeFile(path: string, data: any, options: WriteFileOptions, callback: (err: Error) => void): void; | ||
>writeFile : Symbol(writeFile, Decl(index.d.ts, 1, 38)) | ||
>path : Symbol(path, Decl(index.d.ts, 2, 28)) | ||
>data : Symbol(data, Decl(index.d.ts, 2, 41)) | ||
>options : Symbol(options, Decl(index.d.ts, 2, 52)) | ||
>WriteFileOptions : Symbol(WriteFileOptions, Decl(index.d.ts, 0, 21)) | ||
>callback : Symbol(callback, Decl(index.d.ts, 2, 79)) | ||
>err : Symbol(err, Decl(index.d.ts, 2, 91)) | ||
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
} | ||
|
||
=== /index.js === | ||
import { writeFile, WriteFileOptions, WriteFileOptions as OtherName } from "fs"; | ||
>writeFile : Symbol(writeFile, Decl(index.js, 0, 8)) | ||
>WriteFileOptions : Symbol(WriteFileOptions, Decl(index.js, 0, 19)) | ||
>WriteFileOptions : Symbol(WriteFileOptions, Decl(index.d.ts, 0, 21)) | ||
>OtherName : Symbol(OtherName, Decl(index.js, 0, 37)) | ||
|
||
/** @typedef {{ x: any }} JSDocType */ | ||
|
||
export { JSDocType }; | ||
>JSDocType : Symbol(JSDocType, Decl(index.js, 4, 8), Decl(index.js, 2, 4)) | ||
|
||
export { JSDocType as ThisIsFine }; | ||
>JSDocType : Symbol(JSDocType, Decl(index.js, 4, 8), Decl(index.js, 2, 4)) | ||
>ThisIsFine : Symbol(ThisIsFine, Decl(index.js, 5, 8)) | ||
|
||
export { WriteFileOptions }; | ||
>WriteFileOptions : Symbol(WriteFileOptions, Decl(index.js, 6, 8)) | ||
|
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,33 @@ | ||
=== /node_modules/@types/node/index.d.ts === | ||
declare module "fs" { | ||
>"fs" : typeof import("fs") | ||
|
||
export interface WriteFileOptions {} | ||
export function writeFile(path: string, data: any, options: WriteFileOptions, callback: (err: Error) => void): void; | ||
>writeFile : (path: string, data: any, options: WriteFileOptions, callback: (err: Error) => void) => void | ||
>path : string | ||
>data : any | ||
>options : WriteFileOptions | ||
>callback : (err: Error) => void | ||
>err : Error | ||
} | ||
|
||
=== /index.js === | ||
import { writeFile, WriteFileOptions, WriteFileOptions as OtherName } from "fs"; | ||
>writeFile : (path: string, data: any, options: WriteFileOptions, callback: (err: Error) => void) => void | ||
>WriteFileOptions : any | ||
>WriteFileOptions : any | ||
>OtherName : any | ||
|
||
/** @typedef {{ x: any }} JSDocType */ | ||
|
||
export { JSDocType }; | ||
>JSDocType : any | ||
|
||
export { JSDocType as ThisIsFine }; | ||
>JSDocType : any | ||
>ThisIsFine : any | ||
|
||
export { WriteFileOptions }; | ||
>WriteFileOptions : any | ||
|
55 changes: 55 additions & 0 deletions
55
tests/baselines/reference/jsDeclarationsImportAliasExposedWithinNamespace.errors.txt
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,55 @@ | ||
tests/cases/conformance/jsdoc/declarations/file2.js(1,9): error TS18042: 'myTypes' is a type and cannot be imported in JavaScript files. Use 'import("./file.js").myTypes' in a JSDoc type annotation. | ||
|
||
|
||
==== tests/cases/conformance/jsdoc/declarations/file.js (0 errors) ==== | ||
/** | ||
* @namespace myTypes | ||
* @global | ||
* @type {Object<string,*>} | ||
*/ | ||
const myTypes = { | ||
// SOME PROPS HERE | ||
}; | ||
|
||
/** @typedef {string|RegExp|Array<string|RegExp>} myTypes.typeA */ | ||
|
||
/** | ||
* @typedef myTypes.typeB | ||
* @property {myTypes.typeA} prop1 - Prop 1. | ||
* @property {string} prop2 - Prop 2. | ||
*/ | ||
|
||
/** @typedef {myTypes.typeB|Function} myTypes.typeC */ | ||
|
||
export {myTypes}; | ||
==== tests/cases/conformance/jsdoc/declarations/file2.js (1 errors) ==== | ||
import {myTypes} from './file.js'; | ||
~~~~~~~ | ||
!!! error TS18042: 'myTypes' is a type and cannot be imported in JavaScript files. Use 'import("./file.js").myTypes' in a JSDoc type annotation. | ||
|
||
/** | ||
* @namespace testFnTypes | ||
* @global | ||
* @type {Object<string,*>} | ||
*/ | ||
const testFnTypes = { | ||
// SOME PROPS HERE | ||
}; | ||
|
||
/** @typedef {boolean|myTypes.typeC} testFnTypes.input */ | ||
|
||
/** | ||
* @function testFn | ||
* @description A test function. | ||
* @param {testFnTypes.input} input - Input. | ||
* @returns {number|null} Result. | ||
*/ | ||
function testFn(input) { | ||
if (typeof input === 'number') { | ||
return 2 * input; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
export {testFn, testFnTypes}; |
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
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
Oops, something went wrong.
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.
Why can't it be?
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.
A namespace export should always be an instantiated module (and therefore should have
SymbolFlags.Value
) just like a namespace import is.