Skip to content
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

Document the JSDoc @import tag #3170

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions packages/documentation/copy/en/javascript/JSDoc Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Note:
#### Types

- [`@type`](#type)
- [`@import`](#import)
- [`@param`](#param-and-returns) (or [`@arg`](#param-and-returns) or [`@argument`](#param-and-returns))
- [`@returns`](#param-and-returns) (or [`@return`](#param-and-returns))
- [`@typedef`](#typedef-callback-and-param)
Expand Down Expand Up @@ -198,26 +199,6 @@ function walk(p) {
}
```

import types can be used in type alias declarations:

```js twoslash
// @filename: types.d.ts
export type Pet = {
name: string,
};
// @filename: main.js
// ---cut---
/**
* @typedef {import("./types").Pet} Pet
*/

/**
* @type {Pet}
*/
var myPet;
myPet.name;
```

import types can be used to get the type of a value from a module if you don't know the type, or if it has a large type that is annoying to type:

```js twoslash
Expand All @@ -240,6 +221,28 @@ export const userAccount = {
var x = require("./accounts").userAccount;
```

### `@import`

`@import` can be used for type-only imports:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`@import` can be used for type-only imports:
The `@import` tag can lets us reference exports from other files.


```js twoslash
// @filename: types.d.ts
export type Pet = {
name: string,
};
// @filename: main.js
// ---cut---
/**
* @import {Pet} from "./types"
*/

/**
* @type {Pet}
*/
var myPet;
myPet.name;
```

Comment on lines +244 to +245
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
```
These tags don't actually import files at runtime, and the symbols they bring into scope can only be used within JSDoc comments for type-checking.
```js twoslash
// @filename: dog.js
export class Dog {
woof() {
console.log("Woof!");
}
}
// @filename: main.js
/** @import { Dog } from "./dog.js";
const d = new Dog(); // error!

You'll need to add the appropriate expected error to that code block though.

### `@param` and `@returns`

`@param` uses the same type syntax as `@type`, but adds a parameter name.
Expand Down