DEV Community

Discussion on: Tic Tac Toe with TypeScript - Part 1

Collapse
 
curtisfenner profile image
Curtis Fenner

I don't think you need to have a type declaration for any of those variables; TypeScript's inference is very good, and you generally only need to write type declarations for function parameters.

I haven't written a lot of TypeScript, but I don't think it's idiomatic to include types like string[] when you're clearly assigning a literal oh that type. (Also, that's not nearly specific enough of a type for gameState -- I'm guessing only a few values are valid; maybe it should be (PlayerSign | null)[]; possibly even the tuple so that the length is fixed.

Collapse
 
bornasepic profile image
Borna Šepić

Hey Curtis, thanks for writing back.
As with a lot of things, it really comes down to personal preference.
I've used TypeScript for almost two years now, both at work and on pet projects and I've found it much preferable to just write types for everything rather than have the cognitive load of thinking "this probably doesn't need a type".

It also has an added benefit of improving your IDE experience by offering you better auto completes.

Also, you raise a valid point about the gameState type. To be honest, I wanted to avoid confusing people by rewriting some of the existing logic and just stick to writing types, but this is an excellent place to put the tuples to use. I'll make sure to update it in part two!

Collapse
 
karataev profile image
Eugene Karataev

It also has an added benefit of improving your IDE experience by offering you better auto completes.

Well, for TS lines below are equal. There is no difference in IDE experience (autocomplete, e.t.c).

let gameActive: boolean = true;
let gameActive = true;

I've found it much preferable to just write types for everything rather than have the cognitive load of thinking "this probably doesn't need a type".

Just turn on noImplicitAny option in your tsconfig.json and TS will yell at you where a type can't be inferred. No extra cognitive thinking is required 😂