title: typescript文档7-JavaScript
date: 2023.2.19
tags:


https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html

2023.2.19 星期日

How to use TypeScript-powered JavaScript tooling.

JS Projects Utilizing TypeScript

The type system in TypeScript has different levels of strictness when working with a codebase:

A type-system based only on inference with JavaScript code

JSDoc annotations come before a declaration will be used to set the type of that declaration. For example:

/** @type {number} */
var x;

x = 0; // OK
x = false; // OK?!

@ts-check

To enable errors in your JavaScript files add: // @ts-check to the first line in your .js files to have TypeScript raise it as an error.

If you have a lot of JavaScript files you want to add errors to then you can switch to using a jsconfig.json. You can skip checking some files by adding a // @ts-nocheck comment to files.

TypeScript may offer you errors which you disagree with, in those cases you can ignore errors on specific lines by adding // @ts-ignore or// @ts-expect-erroron the preceding line.

js // @ts-check /** @type {number} */ var x; x = 0; // OK x = false; // Not OK // Type 'boolean' is not assignable to type 'number'. // @ts-expect-error x = false; // Not OK

## Type Checking JavaScript Files
## JSDoc Reference
The list below outlines which constructs are currently supported when using JSDoc annotations to provide type information in JavaScript files.

Creating .d.ts Files from .js files