If we swapped out unknown for any as the type for data, it won’t make any technical difference from a type safety standpoint. Url : string, data : unknown, options : PostOptions = In these situations, we can use the TypeScript unknown type. Usually it’s some pass-through interface that takes arbitrary data, but there are other use cases as well. □ I also enable a bunch of ESLint rules for TypeScript using the particularly the typescript-eslint/no-explicit-any rule that warns when the any type is explicitly used.īut there are legitimate situations where we don’t know the type of a value being passed into a function. Others feel differently about it, which is perfectly fine, but this is how I roll. In my opinion, there’s no point of opting in to the extra work of type-checking if I’m not going to be strict about it. When I’m migrating an app over to TypeScript, I will enable the allowJS option, but within a given file I’m all-TS or nothing. I always turn on the strict option in my tsconfig.json file to receive stronger guarantees of program correctness. I try to be as strict as possible when I use TypeScript. When to use TypeScript unknown vs any How the unknown type in TypeScript helps avoid using the any type for variables we don't know the type of
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |