---
title: "Discussion on \"Typescript Best Practices #1 - Always prefer 'Type Declaration' over 'Type Assertion'\" | Hashnode"
description: "Discussion on \"Typescript Best Practices #1 - Always prefer 'Type Declaration' over 'Type Assertion'\". In TypeScript, there are two ways of assigning a type to a variable.\n\nType Declaration\nType Assertion\n\nIf you have ever written typescript code, you most probably would have used both of them at some point in time. So let's dive deep into what type d..."
url: "https://hashnode.com/posts/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion/612f7c04c21ac123ca3775b7"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Typescript Best Practices #1 - Always prefer 'Type Declaration' over 'Type Assertion'](https://harshitjain.hashnode.dev/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion?utm_source=hashnode&utm_medium=feed)

In TypeScript, there are two ways of assigning a type to a variable. Type Declaration Type Assertion If you have ever written typescript code, you most probably would have used both of them at some point in time. So let's dive deep into what type d...

Sep 1, 2021 · 4 min read · harshitjain.hashnode.dev  
Updated Sep 22, 2021  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#clean-code](https://hashnode.com/tag/clean-code)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#frontend-development](https://hashnode.com/tag/frontend-development)
-   [#javascript](https://hashnode.com/tag/javascript)

## Written by

[Harshit Jain](https://hashnode.com/@harshitjain)

@harshitjain · Noida, India

  
[View profile](https://hashnode.com/@harshitjain)

Frontend @ Intuit | Ex MakeMyTrip, SAP | Javascript| Typescript | React | React Native | UI

I am a Frontend Engineer, based out of India, with 7+ years of building scalable web applications. I am passionate about clean code, design patterns, developer productivity and User Experience

## Responses 2

Markdown supported · ⌘↵ to post

[Hanamant k](https://hashnode.com/@Hanamant)

[Sep 3, 2021](https://hashnode.com/posts/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion/612f7c04c21ac123ca3775b7/comment/6131913b4f9f94786b969680)

Great post..!! In last paragraph of post you mentioned ' Type assertion gives more safety '. is that typo mistake...??

[Harshit Jain](https://hashnode.com/@harshitjain)

[Sep 3, 2021](https://hashnode.com/posts/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion/612f7c04c21ac123ca3775b7/comment/6131913b4f9f94786b969680#reply-6132485a36015b2f4e1283b1)

Thanks for pointing that out. I have fixed it.

[Krzysztof Szala](https://hashnode.com/@AvantaR)

[Sep 2, 2021](https://hashnode.com/posts/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion/612f7c04c21ac123ca3775b7/comment/6130d3720aac045e01493850)

Type Assertion can be helpful in unit tests, when you need to create some typed object, but you don't need all its properties. Otherwise, I try to avoid it because it is almost like typing things with `any`.

[Harshit Jain](https://hashnode.com/@harshitjain)

[Sep 2, 2021](https://hashnode.com/posts/typescript-best-practices-1-always-prefer-type-declaration-over-type-assertion/612f7c04c21ac123ca3775b7/comment/6130d3720aac045e01493850#reply-6130d8a35a73ff5e27bc28c3)

Yes, that's also a common usecase 👍
