DEV Community

Cover image for Typescript Implicit vs Explicit types
Ahmedammarr
Ahmedammarr

Posted on β€’ Edited on

17 1

Typescript Implicit vs Explicit types

The expression implicit used to refer to something done behind the scene, on the other hand, explicit refers to the manual approach of applying things. from here let's discuss implicit and explicit typing in Typescript.
Type is the value that is annotated to a variable and it's compiled in run time, this helps catches errors in an early stage.

Implicit type means that the type is automatically declared for a variable.
So instead of annotating types explicitly

let isTrue: boolean = true;  
Enter fullscreen mode Exit fullscreen mode

we can let the compiler do it for us

let isTrue= true;  

isTrue= 10;

//$ error: Type '10' is not assignable to type 'boolean'
Enter fullscreen mode Exit fullscreen mode

Arrays and objects also can be implicitly typed

let typescriptArr = [1,2]
typescriptArr = ['X','Y']
//$ error: Type 'string' is not assignable to type 'number'.

let typescriptObj = {
userName : 'Ahmed',
age:25
}

typescriptObj.age = 'something'
//$ error: Type 'something' is not assignable to type 'number'.
Enter fullscreen mode Exit fullscreen mode

notes: It's important to use descriptive names when using implicit type.

One more thing about implicit typing, variables declared without value are automatically assigned to type any and we can reassign it to any type.

let anyVar;

anyVar = 'Some String'
anyVar = 10
Enter fullscreen mode Exit fullscreen mode

this will not through any errors, but take into consideration that it's not a good practice and the Typescript community does not encourage the use of any Implicitly or Explicitly

Finally, let's talk about the pros of implicit typing
1- Flexibility: this is one of the Typescript compiler strengths, unlike other strongly typed programming languages, we don't need to define the type for everything in our code.
2- Less Time Consuming: imagine refactoring a codebase with too much explicit typing, It'll take much time and efforts regarding the change of the time

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay