网页
综合
更新时间 2026-11-17 01:33:58
About "slow types" - Docs - JSR
自动蜘蛛
jsr.io
正文约 5985 字
View table of contents
In many of its features, JSR analyzes source code, and in particular TypeScript types in the source code. This is done to generate documentation, generate type declarations for the npm compatibility layer, and to speed up type checking of Deno projects using packages from JSR.
For these features to work, the TypeScript source must not export any functions, classes, interfaces, or variables, or type aliases, that are themselves or reference “slow types”. “Slow types” are types that are not explicitly written, or are so complex that they require extensive inference to be understood.
This inference requires full type checking of the TypeScript source, which is not feasible to do in JSR because it would require running the TypeScript compiler. It is not feasible because tsc does not produce stable type information over time and is too slow to run on every package in the registry on a regular basis (read more in this issue). Because of this, these kinds of types are not supported in the public API.
⚠️ If JSR discovers “slow types” in a package, certain features will either not work or degrade in quality. These are:
“Slow types” occur when a function, class, const declaration, or let declaration is exported from a package, and its type is not explicitly written or the type is more complex that what can be simply inferred.
Some examples of “slow types”:
Slow types that are internal to a package (i.e. are not exported from a file that is in the exports of the package manifest) are not problematic for JSR and consumers of your package. So, if you have a slow type that is not exported, you can keep it as is:
This section lists out all of the restrictions that the “no slow types” policy imposes on TypeScript code:
All exported functions, classes, and variables must have explicit types. For example, functions should have an explicit return type, and classes should have explicit types for their properties, and constants should have explicit type annotations.
Module augmentation and global augmentation must not be used. This means that packages cannot use declare global, declare module, or export as namespace to augment the global scope or other modules.
CommonJS features must not be used. This means that packages cannot use export = or import foo = require("foo").
All types in exported functions, classes, variables, and types must be simply inferred or explicit. If an expression is too complex to be inferred, its type should be explicitly assigned to an intermediate type.
Destructuring in exports is not supported. Instead of destructuring, export each symbol individually.
Types must not reference private fields of classes.
Expando properties (dynamically assigned properties on objects or functions) are not supported.
All symbols exported from a package must explicitly specify types. For example, functions should have an explicit return type:
Classes should have explicit types for their properties:
Constants should have explicit type annotations:
Module augmentation and global augmentation must not be used. This means that packages cannot use declare global to introduce new global variables, or declare module to augment other modules.
Here are some examples of unsupported code:
CommonJS features must not be used. This means that packages cannot use export = or import foo = require("foo").
Use ESM syntax instead:
All types in exported functions, classes, variables, and types must be simply inferred or explicit. If an expression is too complex to be inferred, its type should be explicitly assigned to an intermediate type.
For example, in the following case the type of the default export is too complex to be inferred, so it must be explicitly declared using an intermediate type:
Or using an as assertion:
For super class expressions, evaluate the expression and assign it to an intermediate type:
Destructuring in exports is not supported. Instead of destructuring, export each symbol individually:
Types must not reference private fields of classes during inference.
In this example, a public field references a private field, which is not allowed.
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:
To reference a .d.ts type declaration file from a JavaScript entrypoint, there are two options:
Note: Simply placing a .d.ts file alongside a .js file with the same name (e.g. mod.js and mod.d.ts) is not sufficient. You must explicitly reference the .d.ts file using one of the directives below. Without the directive, JSR will attempt to infer types from the JavaScript file itself and report slow type warnings.
If a TypeScript module in your package imports a JavaScript module that does not have type declarations, JSR will not be able to type-check the import. This is because fast check avoids type inference, so any JavaScript file referenced from TypeScript must have corresponding type declarations.
To fix this, you have two options:
If the JavaScript module is an external npm dependency, ensure it ships type declarations (a .d.ts file), or install a corresponding @types/ package.
This error occurs when an export listed in the exports field of your jsr.json or deno.json(c) cannot be resolved. This is usually caused by a typo in the path, or by a mismatch between the exports field and the actual file structure of your package.
To fix this, verify that all paths in your exports field point to files that actually exist in your package:
Expando properties (dynamically assigned properties on objects or functions) are not supported by fast check because their types cannot be statically determined without running the code.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。