We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
project |- src |- index.js |- package |- module.js |- module.d.ts
index.js
import
// module.js export function foo(x) { return x; }
// module.d.ts export declare function foo(x: number): number;
// index.js import { foo } from './src/module'; // foo 的类型是 number -> number // 因为有一个同名的 d.ts 文件,TypeScript 可以找到相对应的类型, // 但是生成 JS 文件中 `foo` 还是会正确的指向 module.js 中的 `foo` foo
// module.js // foo 的类型是 any -> any export function foo(x) { return x; }
因为 module.js 和 module.d.ts 是两个不同的 module(指 ES 中的 module), 他们有属于自己的作用域。
目前只能通过在 js-doc 中使用动态 import 的语法,把 foo 联系起来。
foo
// module.js /** * * @type {import("./module").foo} * 这里 foo 的类型是 number -> number */ export function foo(x) { return x; }
如果 module 里有多个方法,那么
/** * * @typedef {import("./module")} Module */ /** * * @type {Module["foo"]} */ export function foo(x) { return {} }
未来可能会引入新的 js-doc tag named @import and @from, 好处是看起比上面的 intuitive.
@import
@from
有两种 proposals:
/** * * @from "./module" * @import { foo } from * @import Default * @import * as ns */
/** * @import { foo, y as z, default as Default } from "./module" */
这么麻烦,还不如直接写 TypeScript...
microsoft/TypeScript#14342 microsoft/TypeScript#14377 microsoft/TypeScript#14844 microsoft/TypeScript#22160
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
文件结构
project
|- src
|- index.js
|- package
|- module.js
|- module.d.ts
在
index.js
中import
module然而 module.js 并不能享受到 module.d.ts 中的类型提示
因为 module.js 和 module.d.ts 是两个不同的 module(指 ES 中的 module), 他们有属于自己的作用域。
让 module.js 也享受 module.d.ts 的类型提示
目前只能通过在 js-doc 中使用动态
import
的语法,把foo
联系起来。如果 module 里有多个方法,那么
神 TM 展望未来
未来可能会引入新的 js-doc tag named
@import
and@from
, 好处是看起比上面的 intuitive.有两种 proposals:
Conclusion
这么麻烦,还不如直接写 TypeScript...
Reference:
microsoft/TypeScript#14342
microsoft/TypeScript#14377
microsoft/TypeScript#14844
microsoft/TypeScript#22160
The text was updated successfully, but these errors were encountered: