-
Notifications
You must be signed in to change notification settings - Fork 0
Any type
We already know that TypeScript is able to infer the types of assigned variables when we declare those. So the snippet of code below would warn us about typecheck errors because it was infered that name is a string, and so we could not reasign it to a number or try to run array methods over it.
let name = 'bob';
name = 1;
name.map(() => {
/* do something **/
});But what would happen if we manually set the type declaration of name to any? Well essentially, we are disabling the typecheck features provided by TypeScript. Even though that sounds a good idea at the begining, that should be avoied as much as possible, because we, as devs, want our codebase to be as type-safe as possible.
A short answer to that would be: We shouldn't. Even though it's it's tempting to just manually type everything as any so all the typechecks errors go away, why then use TypeScript in the forst place if we are using the type any?
There is though a particular situation where the use of any as type "is permitted": when we are working on the process of transitioning our codebase from JavaScript to TypeScript and we need a temporary solution for the lack of immediate time to write all the required type checking on the code.
Even though there is a better approach to this, which is using another special type unknkown which will be covered in future sections.
Those notes were written while watching the tutorial videos while taking the classes from the online course Learn TypeScript on Scrimba.
Because english is not my mother language, they can contain some typos and everything written here is based on my understanding about the discussed topics and may not be 100% accurate.
If you want the full course, support the instructor by buying their course on Scrimba.
- Home
- Introduction
- Introduction to TypeScript
- The Pizza Application
- Move to TypeScript
- Defensive Coding
- Typing variables
- Typing Pizza App: part 1
- Custom types
- Typing Pizza App: part 2
- Nested Object types
- Optional Properties
- Typing Pizza App: part 3
- Array Types
- Typing Pizza App: part 4
- Literal Types
- Unions
- Typing Pizza App: part 5
- Typing Pizza App: part 6
- Typing Pizza App: part 7
- Returning Types
- Typing Pizza App: part 8
- Any Type
- Typing Pizza App: part 9
- Utility Types
- Typing Pizza App: part 10
- Generics
- Typing Pizza App: part 11