![]() ![]() net 1.0 beta days, you can imagine I am pretty comfortable in a static languages and while that is fine most of the time the trend is for more apps to run in the browser. A new JavaScript superset which adds static typing annotations as well as language support for classes and modules to the JavaScript language. Basically now, we have a better shot of picking up on that problem at build time.Last week Microsoft announced TypeScript. In our case for the RGB staff, we might use a never in a situation where you're passing in like a string that is neither of size 3 or size 6. So you wanna, it's for scenarios you're deliberately trying to avoid. It is the compile time you think of it that way. And so this function is gonna return a never and you're trying to capture the result, and that's gonna be a never. I expect that you're gonna end up with a never here. Where if somehow you have a function that in some situation returns a never, now as the compiler is analyzing your code, it's gonna say, well okay based on the argument types that are passed in. You can almost think of it like a throw except for the typescript compiler. But just think of it as something you would want in potentially unreachable sections of code. You might ask, why would you do this? We'll dig into it a little bit tomorrow as we talk about types in a deeper way. > Mike North: This is not, nothing can be assignable to a never. > Mike North: So if we had something like this, The opposite of the any type is a never type. And then, you would try to purge explicit any's from your code base, and end up with things having a well defined type instead of sort of the catchall. And you'd go through and say, okay, well where we really wanna be flexible, we will have explicit any's, or we'll give it a type, a proper type. So then, you would at some point, forbid implicit any's. ![]() I can't figure it out, I don't have enough information in order to make a call here. And you return a + b, those could be strings, those could be numbers, I don't know. Right, where it's gonna go through your code and it's gonna say, all right, this is a function that takes two arguments, and it's a and b. You would end up with a lot of implicit any's. You would say anything goes, we're just gonna change this JS file to TS. Typically what you do is, say we have a JavaScript piece of code and we're converting it to TypeScript. And as you tighten things up, you can sort of make that constraint more accurate and more tightened up to what you actually expect the range of those values to be. It'll just give it the any type and it'll allow things to play out from there. Where it can't infer the type of a variable. ![]() This is a big part of how it's able to do that. And I talked about how you could have typescript to kind of chew on regular JavaScript code and make sense of it, and give you some information. This is the way a non typescript world treats things like any variable that's defined with let. The way you can think of this is, you're back to regular JavaScript. We can give it values of whatever we want. And then we have myAge, and we say age as any. Anything could be assigned to a variable that is of the any type. And think of this as a wild card type, right. > Mike North: So let me add one more interesting thing here before we jump in to our next exercise. Transcript from the "any & never Types" Lesson
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |