Possible to disable type checking?
Asked Answered
B

3

41

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.

Branson answered 27/6, 2015 at 8:41 Comment(0)
I
17

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).

Interfertile answered 27/6, 2015 at 14:10 Comment(1)
concerning @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.9Elliotelliott
W
60

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
Whereabouts answered 23/11, 2019 at 2:13 Comment(0)
I
17

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).

Interfertile answered 27/6, 2015 at 14:10 Comment(1)
concerning @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.9Elliotelliott
D
2

Yes. Types are optional by default unless the noImplicitAny compiler option has been enabled in your project.

Dogvane answered 27/6, 2015 at 8:55 Comment(4)
Sorry for having been unclear it seems, I want typescript not to check types whatsoever. It seems you understood I want to use the type "any" ...Branson
Maybe you are better off using ES6 and transpilng it ES5 with Babel or Traceur. Typescript without types is essentially ES6Dogvane
Thanks Martin, I wasn't aware of Traceur. Nevertheless I am happy with Typescript, also as angularjs is going there with 2.0, but there are occasions in which i would love to disable type checking for various reasons.Branson
Also noImplicitThis and noImplicitReturn. @Dogvane Sometimes I need to turn type checking off temporarily, since I have to use a library whose type definition file is not production ready.Stcyr

© 2022 - 2024 — McMap. All rights reserved.