Wrong. unknown can be a pain in the ass and you'd have to write 5 millions lines of code to narrow the type. Not worth wasting time writing that code and the potential performance hit.
According to our front-end developers, the appropriate time to use any is any time you can't be bothered to create a type for a semi complicated value from the backend.
Looking forward to it. I hate the any keyword and I would never use it personally. But since you are saying it has its use cases, I am intrigued, coz you are the one whom I learned typescript from. 😂
Typescript is hard to fathom for some juniors. Because they feel like they declare classes. But then suddenly at runtime they they a string instead of a number in a value because of their API returned it. And it contains other values not used. Makes it really confusing for them. Sometimes I wish it wasn't so.
I want to know if there is a better way to do things like: const array: GenType[] = [ ..stuff.. ] We need 'any' to be able to put different GenType values into the array, but using 'any' breaks internal type checking in those items.
TypeScript describes itself as a superset of JavaScript. In other words TS adds new features to JS, but still stays compatible with it. This is one of the reasons why TypeScript became so popular - it can be used seamlessly with JS, which means you can migrate from JS to TS at any moment and learning curve is very gradual. Hell, you can simply change file extension from ".js" to ".ts" and everything will work without any refactor. 100% strict & mandatory type checking would break this compatibility and make TS migration for most projects almost impossible. Just imagine rewriting a large project from JS to Dart.
It's not quite the same thing. `any` means that the compiler won't enforce anything, and every operation is permitted. it should be used when the type *can't* be represented in typescript (i.e. never use it, unless you interop with a library that does awful things with the type system). `Object` means we don't know anything about this object, so everything is enforced. you cannot pass this as an argument as any other type (whereas ts `any` can be passed as any other type), and you can't *do* anything with it, except compare a few universal operations that are required on all java types. Java has a typed runtime, so it inherently can't interact with unrepresentable types, because they cannot exist (unlike in TS) `any` can cover all the use cases of Java's `Object`, but the inverse isn't quite true. I'm not aware of a great equivalent in Java, but the closest in C# might be `dynamic` which behaves just like TypeScript's `any`. But C# also runs on a typed runtime, so how can this be? Well, `dynamic` uses reflection to access fields on the value. of course the individual object contained is always representable, but the "real" type with semantic meaning in the context of this variable may not, i.e. if some condition outside of the type system determines whether or not a field exists at all, or if its name is likewise dependent on an external condition
Bad practice: Use "any" anywhere you like. Good practice: Add "@typescript-eslint/no-explicit-any" to ESLint rules and add an exception where "any" is really needed.
JS itself is too dynamic for TypeScript to be fully type-safe. It's not so rare to have a function that takes literally just any type and determines what to do with it based on some internal checks... For example, you might have a logger that concatenates multiple strings and numbers or extracts message and stack from errors, stringifies objects or transforms dates to your liking, then if none of the cases matched, logs the thing straight away. Strictly typing such a thing would just be a nightmare.
love the fit. Going from typescript king to drip king
Unknown should (my preference: must) be used over any
It must.
Not always possible, especially when dealing with generics
@@robertsandiford6223 trying to think of an example. Do you have one?
Wrong. unknown can be a pain in the ass and you'd have to write 5 millions lines of code to narrow the type. Not worth wasting time writing that code and the potential performance hit.
@@soniablanche5672then just use JavaScript directly
Because every real language has a footgun.
According to our front-end developers, the appropriate time to use any is any time you can't be bothered to create a type for a semi complicated value from the backend.
Any is litterally my nemesis at work atm , hehe. looking forward to the next vid.
Looking forward to it. I hate the any keyword and I would never use it personally. But since you are saying it has its use cases, I am intrigued, coz you are the one whom I learned typescript from. 😂
never and unknown together are just better alternatives
its so we can just put any everywhere and add typescript to our resume
Typescript is hard to fathom for some juniors. Because they feel like they declare classes. But then suddenly at runtime they they a string instead of a number in a value because of their API returned it. And it contains other values not used.
Makes it really confusing for them. Sometimes I wish it wasn't so.
I want to know if there is a better way to do things like:
const array: GenType[] = [ ..stuff.. ]
We need 'any' to be able to put different GenType values into the array, but using 'any' breaks internal type checking in those items.
satisfies
@@mattpocockuk my current approach is
const array = [
{ first: 'A', second: 'B' } satisfies GenType,
{ first: 'C', second: 'D' } satisfies GenType
] as const satisfies GenType[]
But it's very wordy and repetitive
@@mattpocockuk Realised I was being a bit dumb, and can do
satisfies GenType[]
It's better to use an occasional any than to ditch TypeScript because it's to frustrating.
👍🏻
You cold?
TypeScript describes itself as a superset of JavaScript. In other words TS adds new features to JS, but still stays compatible with it. This is one of the reasons why TypeScript became so popular - it can be used seamlessly with JS, which means you can migrate from JS to TS at any moment and learning curve is very gradual. Hell, you can simply change file extension from ".js" to ".ts" and everything will work without any refactor. 100% strict & mandatory type checking would break this compatibility and make TS migration for most projects almost impossible. Just imagine rewriting a large project from JS to Dart.
Java have object type. It does same thing
It's not quite the same thing. `any` means that the compiler won't enforce anything, and every operation is permitted. it should be used when the type *can't* be represented in typescript (i.e. never use it, unless you interop with a library that does awful things with the type system).
`Object` means we don't know anything about this object, so everything is enforced. you cannot pass this as an argument as any other type (whereas ts `any` can be passed as any other type), and you can't *do* anything with it, except compare a few universal operations that are required on all java types. Java has a typed runtime, so it inherently can't interact with unrepresentable types, because they cannot exist (unlike in TS)
`any` can cover all the use cases of Java's `Object`, but the inverse isn't quite true. I'm not aware of a great equivalent in Java, but the closest in C# might be `dynamic` which behaves just like TypeScript's `any`. But C# also runs on a typed runtime, so how can this be? Well, `dynamic` uses reflection to access fields on the value. of course the individual object contained is always representable, but the "real" type with semantic meaning in the context of this variable may not, i.e. if some condition outside of the type system determines whether or not a field exists at all, or if its name is likewise dependent on an external condition
Bad practice: Use "any" anywhere you like.
Good practice: Add "@typescript-eslint/no-explicit-any" to ESLint rules and add an exception where "any" is really needed.
Why does Typescript exist in any?? How about that??
Cause js is a mess hahaa
Very neo philosophical
JS itself is too dynamic for TypeScript to be fully type-safe. It's not so rare to have a function that takes literally just any type and determines what to do with it based on some internal checks...
For example, you might have a logger that concatenates multiple strings and numbers or extracts message and stack from errors, stringifies objects or transforms dates to your liking, then if none of the cases matched, logs the thing straight away. Strictly typing such a thing would just be a nightmare.
No that is perfectly possible to do in a typesafe way, even preferable, using the unknown type.
i dont understand a single part of this video