Using JSDoc for Type Checking in JavaScript: Why Svelte Switched from TypeScript
This article explains how JSDoc can provide TypeScript‑like type checking, documentation generation, and d.ts emission for plain JavaScript, demonstrates the required tsconfig settings and code examples, and reveals that Svelte adopted JSDoc mainly to simplify debugging without a compilation step.
In recent months many developers have heard that Svelte abandoned TypeScript in favor of JSDoc. The article first clarifies what JSDoc is: a comment‑based syntax that can be processed by the TypeScript compiler to add type annotations, generate documentation, and produce declaration files.
TypeScript supports JSDoc annotations inside .js files, allowing developers to write type information directly in comments. By enabling allowJS and checkJS in tsconfig.json , the compiler performs type checking on JavaScript files just like it does on TypeScript files.
Example commands to set up a project are shown:
mkdir jsdoc-test
cd jsdoc-test
npm init -y
npm install --save-dev typescript
npx tsc --initAfter configuring tsconfig.json to include the source files, a simple function can be written in src/index.ts and compiled with npx tsc . The same approach works for a src/index2.js file when JSDoc comments such as @param {number} a and @param {number} b are added, and the compiler will report type errors once checkJS is enabled.
Enabling declaration (or dts ) in the config allows the compiler to emit .d.ts files for JavaScript sources, providing type definitions without altering the original code.
JSDoc can express a wide range of TypeScript constructs: variable types with @type , reusable type aliases with @typedef , generic functions and classes with @template , and even complex type programming. These definitions can be imported from separate .d.ts files using @type {import('xxx').xxx} .
The article demonstrates generic class definitions, optional parameters, default values, and return types using JSDoc syntax, showing that everything TypeScript can express is also possible with JSDoc.
Why did Svelte choose JSDoc? The official discussion reveals that using TypeScript syntax requires a compilation step and source‑map generation for debugging, whereas JSDoc allows direct debugging of JavaScript without an extra compile step, simplifying the development workflow.
In summary, JSDoc provides non‑intrusive type annotations for JavaScript, offering the same type checking, IntelliSense, and declaration file generation as TypeScript, but without the need for compilation, which is why projects like Svelte have adopted it.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.