Is it possible to disable type checking when using TypeScript? I love TypeScript for classes, interface etc. yet for the smaller one person projects I am normally engaged in I really don't need type checking and not finding ready made type definitions for less common libraries or the latest version of it is a pain.
In TypeScript, types may be optional when defined that way.
Since it seems like the biggest pain you are having is finding type definitions for external libraries, you can create an ambient definition for any variable you don't want to type checking for:
declare var variableName: any;
For example, for jQuery it would be declare var $: any;
. Then you could do: $("#test").myNonExistentFunction();
if you wanted.
Alternatively, when using es2015 modules the following code could be done to allow the library to be imported:
declare module "jquery" {
var _temp: any;
export = _temp;
}
Shorthand ambient module declarations (TS 2.0+)
In TS 2.0+, a better way to disable type checking for imports is to create a .d.ts
file in your project and define shorthand ambient module declarations:
declare module "jquery";
// or use a wildcard
declare module "express-*"; // or use "*" to allow everything
This will allow you to use those imports freely without type checking:
import $ from "jquery";
$.something(); // ok at compile time, but will be an error at runtime
That said, the best path to take in this scenario is in a .d.ts
file in your project to gradually define the interface of the library you're depending on based on what you use.
ts-ignore comments (TS 2.6+)
It's possible to disable any TypeScript error by using // @ts-ignore
comments in TypeScript 2.6+. For example:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
That might lead to unexpected behaviour when using the compiler though because it's not well tested. I'd recommend against this approach unless absolutely necessary.
Transform with Babel
If you don't need type checking, another option is to use Babel. It supports TypeScript's syntax (see here).
In TypeScript 3.7, type checking will be disabled on a file if it starts with the following comment:
// @ts-nocheck
As already mentioned, in previous versions you can disable checking on a single line by including the following comment on the previous line:
// @ts-ignore
In TypeScript, types may be optional when defined that way.
Since it seems like the biggest pain you are having is finding type definitions for external libraries, you can create an ambient definition for any variable you don't want to type checking for:
declare var variableName: any;
For example, for jQuery it would be declare var $: any;
. Then you could do: $("#test").myNonExistentFunction();
if you wanted.
Alternatively, when using es2015 modules the following code could be done to allow the library to be imported:
declare module "jquery" {
var _temp: any;
export = _temp;
}
Shorthand ambient module declarations (TS 2.0+)
In TS 2.0+, a better way to disable type checking for imports is to create a .d.ts
file in your project and define shorthand ambient module declarations:
declare module "jquery";
// or use a wildcard
declare module "express-*"; // or use "*" to allow everything
This will allow you to use those imports freely without type checking:
import $ from "jquery";
$.something(); // ok at compile time, but will be an error at runtime
That said, the best path to take in this scenario is in a .d.ts
file in your project to gradually define the interface of the library you're depending on based on what you use.
ts-ignore comments (TS 2.6+)
It's possible to disable any TypeScript error by using // @ts-ignore
comments in TypeScript 2.6+. For example:
if (false) {
// @ts-ignore: Unreachable code error
console.log("hello");
}
That might lead to unexpected behaviour when using the compiler though because it's not well tested. I'd recommend against this approach unless absolutely necessary.
Transform with Babel
If you don't need type checking, another option is to use Babel. It supports TypeScript's syntax (see here).
@ts-ignore
, there is an alternative @ts-expect-error
. Typescript will nicely error if the comment is not actually shadowing an error. Also Eslint has a rule which expects a comment next to it to explain why you need to disable type checking. typescript 4.9 –
Elliotelliott Yes. Types are optional by default unless the noImplicitAny compiler option has been enabled in your project.
© 2022 - 2024 — McMap. All rights reserved.
@ts-ignore
, there is an alternative@ts-expect-error
. Typescript will nicely error if the comment is not actually shadowing an error. Also Eslint has a rule which expects a comment next to it to explain why you need to disable type checking. typescript 4.9 – Elliotelliott