Skip to content

Commit

Permalink
Add docs for JS entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Jan 23, 2025
1 parent d69430c commit 0bd90c7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
58 changes: 58 additions & 0 deletions frontend/docs/about-slow-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,64 @@ allowed.
+ type MyPrivateMember = string;
```

## JavaScript entrypoints

If a package has a JavaScript entrypoint, JSR will not be able to create type
definitions for the package. JSR only generates type definitions for TypeScript,
and not for JavaScript. Using JavaScript entrypoints is not a fatal error: JSR
will not be able to generate type definitions for the package, and will not show
documentation for the package - but the package will still be usable.

There are two ways to fix this:

1. Convert the JavaScript entrypoint to TypeScript. This involves renaming the
file from `.js` to `.ts` and adding types.
2. Reference a `.d.ts` type declaration file from the JavaScript entrypoint, so
that JSR can use the type declaration file to generate types. This is useful
if you cannot convert the JavaScript entrypoint to TypeScript, or if the code
is generated.

To reference a `.d.ts` type declaration file from a JavaScript entrypoint, there
are two options:

1. In the file itself, add a `/* @ts-self-types="./path/to/types.d.ts" */`
directive at the top of the file. This instructs JSR to use the types from
the specified file, rather than using the file itself.
2. Wherever the file is imported (e.g. in an import statement), add a
`/* @ts-types="./path/to/types.d.ts" */` directive directly above the import
statement. This instructs JSR to use the types from the specified file,
rather than using the file itself when generating types or documentation.

```js
// index.js
/* @ts-self-types="./index.d.ts" */
export function foo() {
return "foo";
}
```

```ts
// index.d.ts

export function foo(): string;
```

OR

```js
// foo.js
/* @ts-types="./bar.d.ts" */
import { foo } from "./bar.js";

// bar.js
export function foo() {
return "foo";
}

// bar.d.ts
export function foo(): string;
```

## Simple inference

In a few cases, JSR can infer a type without you needing to specify it
Expand Down
1 change: 1 addition & 0 deletions frontend/docs/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const slowTypesRedirects = new Map([
],
["unsupported-ts-export-assignment", "#commonjs-features"],
["unsupported-ts-namespace-export", "#global-augmentation"],
["unsupported-javascript-entrypoint", "#javascript-entrypoints"],
]);

export function go(id: string): string | null {
Expand Down

0 comments on commit 0bd90c7

Please sign in to comment.