TYPE- any in ts - DAY3 OF 100DAYSOFCODE

TYPE- any in ts - DAY3 OF 100DAYSOFCODE

Any type in typescript

in the previous article, if you remember the last moment, if we don't declare and initialize value in the same line then it will return any type.

I hope you also remember, that we can give type by ourselves ( type annotation) or typescript will auto assume type ( inference ). so any type is also possible with both. either we explicitly mention type to any variable or if typescript doesn't understand type then it will inference any type. let's understand from the below example.

No alt text provided for this image

here, we are set to type to isExists to any. same as we set type age to number. now let's understand for inference. if you are still confused about inference then kindly check my last blog.

if you check age2 typescript is set automatically to type to number but in isProductExists, it is set to any. why!!

because JSON.parse() function return type depends on given params like it returns boolean, number, object anything. so here typescript is not aware of the return type. so if typescript doesn't understand the exact output type then it will give back any type.

No alt text provided for this image

We can understand any type is the same as plain javascript. you can also change the value to a string, a number, or any other type which will not give any error also while compiling ts file to js.

No alt text provided for this image

did you notice? we are assigning random parmas, still, it does not give a red underline. if we set it to boolean then it will definitely give a red underline because we do not use like objects.

in short, you should avoid any types in all scenarios otherwise it makes no sense to use typescript. typescript is used to understand the type of data. so let's understand how to avoid this scenario.

Fix any type:

  • when you declare and initialise variable in diff line.

No alt text provided for this image


Here isMeetExists has any type because we are assigning value to a later stage. you also notice that we are assigning "hehehe" in the last line and ts not gives red underline.

in this type of scenario always gives types when you declare you also declare type also like,

No alt text provided for this image

Now typescript gives an error that "Property 'hehehe' does not exist on type 'boolean'".

  • If you are using JSON.parse() then don't forget to assign a type.

let age = JSON.parse('24')
age = "twenty four"  // will not give error

let age1: number  JSON.parse('24')  
age1 ="twenty four"   // will gives error        

  • give noImplicitAny: true in tsconfig.ts

No alt text provided for this image


Thank you for reading!!.

要查看或添加评论,请登录

Meet Vaghasiya的更多文章

  • START EXPRESS JS WITH TYPESCRIPT

    START EXPRESS JS WITH TYPESCRIPT

    In this article, we will learn How to install express js with typescript and minimal setup configuration of…

  • Redux with typescript

    Redux with typescript

    Let's see how we use redux with typescript install react typescript app npx create-react-app typescript-practise…

  • Using typescript with react.

    Using typescript with react.

    Here we will learn step by step process for starting the #typescript with a #reactjs . Steps: create a typescript app.

  • Index signature and keyof operator in Typescript

    Index signature and keyof operator in Typescript

    Index signature Let's say we have two types of objects. and you want to make a common function that returns total marks.

  • Use generics like pro

    Use generics like pro

    There is a rule in programming called DRY- don't repeat yourself. generics help us to use this convention in typescript.

  • High-level overview of type and interface in Typescript

    High-level overview of type and interface in Typescript

    Standard javascript object is a map of key: value pairs. When we define an object with properties (keys) and values…

  • Classes in typescript - 6thday of 100daysofcoding

    Classes in typescript - 6thday of 100daysofcoding

    Typescript is fully supported to work with modern javascript keyword class. Generally, classes are blueprints to make…

  • Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions and object in typescript- DAY5 OF 100DAYSOFCODE

    Functions: Functions are a very important part of any programming language as it provides reusability. let's see, how…

  • ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY AND TUPLES IN TYPESCRIPT - DAY4 OF #100DAYSOFCODE

    ARRAY IN TYPESCRIPT In typescript, typescript need to know type of each items in array. so it will tell us error if we…

  • Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    Type and Type system in Typescript - DAY2 of 100 DAYSOFCODE

    What is a type system? When we learn any programming languages, first they introduces with various data-types of that…

社区洞察

其他会员也浏览了