Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In idiomatic Typescript, should I always declare a variable's type, or should I rely more on type inference?

At first, our team found ourselves writing lots of code like this because that's what we're used to in languages like ActionScript.

var arrayOfFoo : Array<Foo> = new Array<Foo>();
//Then, sometime later:
var someFoo : Foo = arrayOfFoo[0];
someFoo.someFooMethod();

That's fine, but it can be simplified by relying more heavily on Typescript's type inference:

//No need to declare the type ": Array<Foo>" here:
var arrayOfFoo = new Array<Foo>();

//Again, no need to declare that someFoo is a Foo
var someFoo = arrayOfFoo[0];
someFoo.someFooMethod();

Typescript is pretty darn good at type inference. If I drop the type from the left-hand side of assignments, the compiler still knows what type that object is, and will still give a compile error if I try to do something on the variable the inferrred type can't do.

I like that it's less code to read, and less code to type. The example that declares types is beginning to feel "redundant" to me, but I'm worried that we might be setting ourselves up for trouble later. I'm curious what the community recommends, if anything.

like image 786
Taytay Avatar asked Jul 03 '14 17:07

Taytay


People also ask

Do you have to declare type in TypeScript?

When to declare type to variables in TypeScript? The answer is when TypeScript is able to do the type guessing. We're just going to rely on it as much as possible. It makes no sense to write unnecessary characters when we don't have to.

Should you type everything in TypeScript?

Yes, you should make it a habit to explicitly set all types, it's will prevent having unexpected values and also good for readability.

Which is the correct method to declare a variable in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable.

Which of the following is correct for the infer types in TypeScript?

Types are inferred by TypeScript compiler when: Variables are initialized. Default values are set for parameters. Function return types are determined.


1 Answers

What I do for my project is not putting the type definition everywhere when it can be inferred because (as you already said) it is redundant.

What I'm currently not doing (but I really want to do it) is compiling with the --noImplicitAny any flag.

With that flag enabled you will get an error where it couldn't infer the type which is really helpful! You might want to look at that! See my example below.

The code below will give three errors when compiled with tsc --noImplicitAny tst.ts:

var arr = [];
var x = null;
var a: string[] = [];
var s = a["foo"]

tst.ts(1,11): error TS7015: Array Literal implicitly has an 'any' type from widening.

tst.ts(2,5): error TS7005: Variable 'x' implicitly has an 'any' type.

tst.ts(5,11): error TS7017: Index signature of object type implicitly has an 'any' type.

This way when you doing something weird (accidentally) will give an error.

like image 111
Dick van den Brink Avatar answered Sep 29 '22 14:09

Dick van den Brink