ts-check-jsdoc-experiment
Use @ts-check and JSDoc to Experiment with TypeScript
Overview
You can add TypeScript type checking to JavaScript files without converting them to TypeScript. Use @ts-check at the top of a JS file and JSDoc annotations to add types. This lets you experiment with TypeScript gradually without committing to the full conversion.
When to Use This Skill
- Experimenting with TypeScript
- Gradually migrating JavaScript
- Adding types to JS files
- Teams learning TypeScript
- Validating JavaScript with types
The Iron Rule
Use @ts-check and JSDoc to add TypeScript checking to JavaScript files without full conversion.
Example
// @ts-check
/**
* @param {string} name
* @param {number} age
* @returns {string}
*/
function greet(name, age) {
return `Hello ${name}, you are ${age}`;
}
greet('Alice', 30); // OK
greet('Alice', '30'); // Type error!
/** @type {string[]} */
const names = ['Alice', 'Bob'];
/** @typedef {{ x: number, y: number }} Point */
/** @type {Point} */
const point = { x: 1, y: 2 };
Reference
- Effective TypeScript, 2nd Edition by Dan Vanderkam
- Item 80: Use @ts-check and JSDoc to Experiment with TypeScript
More from marius-townhouse/effective-typescript-skills
precise-any-variants
Use when forced to use any. Use when any is too broad. Use when function types need any.
86narrow-any-scope
Use when any is unavoidable. Use when working with untyped libraries. Use when silencing specific type errors.
35tsdoc-comments
Use when documenting public APIs. Use when writing library code. Use when using JSDoc-style comments. Use when generating documentation. Use when explaining complex types.
33exhaustiveness-checking
Use when handling tagged unions. Use when adding new cases to discriminated unions. Use when switch statements must cover all cases.
13code-gen-independent
Use when confused about types at runtime. Use when trying to use instanceof with interfaces. Use when type errors don't prevent JavaScript output.
12tsconfig-options
Use when setting up a TypeScript project. Use when confused by type checking behavior. Use when strict mode causes unexpected errors.
11