Just Like I Wanted

Just Like I Wanted

So for this afternoon - on bootcamp, YAY - I am teaching myself TypeScript.

And oh and for reference - this blog post title is as always from my favourite song of all time, 'Yosemite' by Lana del Rey.

Because it's a song about loving something forever and that is exactly how I feel about software engineering.

Yosemite national park. A valley with cliffs and trees in the middle of it. Some of the leaves on the trees are orange so it may be autumn.

Type Shapes

Eh? WHAT ON EARTH IS THIS??!

"An object's shape describes, among other things, what properties and methods it does or doesn't contain."

Eh? I'm sorry Codecademy? What does this mean please?

"Because TypeScript knows that types our objects are, it also knows what shapes our objects adhere to".

EH?

Some more examples of this please?

"The built-in types in JavaScript each have known properties and methods that always exist." Ah yes yes. I see.

"All strings, for examples, are known to have a .length property and a .toLowerCase() method".

**************************************

"TypeScript's tsc command will let you know if your code tries to access properties and methods that don't exist".

EH? AS IF THIS NEEDED TO GET ANY MORE COMPLICATED?

"Through this knowledge of type shapes, TypeScript helps us quickly locate bugs in our code.

Okay yeah whatever I'm off to do some exercises.

Okay but this is crazy

Here is an example of the really dumb errors that TypeScript can fix for you.


Any

Okay so before I dive into this next lesson, I want to confirm that I have heard of the any type.

I think you use it for when you want to type loosely - otherwise, TypeScript auto-assigns a type for you based on your initial variable declaration. 

I have seen it in various videos and stuff.

In situations where TypeScript "isn't able to infer a type", TypeScript will "consider a variable to be of type any".


In this example from Codecademy: the variable is declared without a type. 

So TypeScript infers its type to be any.

Variable Type Annotations 

Hmm just when I was beginning to get confident... along comes a scary title LIKE THIS.

Variable Type Annotations, attempt no. 2: I'm not scared, you're scared

This is for when we initialise a variable with no current value - but we want to be sure that this variable will only be reassigned to values of a certain type.

"We can tell TypeScript what type something is or will be by using a type annotation".

Hmmmm.

Hmmm, hmmm, hmm, hmmmm.

"Variables can have type annotations (also know as type declarations) added just after their names". YAYYYYYYYYYYYYYYYYYYYYYYYYY

This is the good bit. This is the bit I have been waiting for.

"We provide a type annotation by appending a variable with a colon (:) and the type (e.g., number, string, or any).

It's been a while since I made my own example up... well here goes!

let makeThisVariableAStringOrElse : string;

makeThisVariableAStringOrElse = 'AWWWWWWWW YEAH';

makeThisVariableAStringOrElse = 27;
// Error: Type 'number' is not assignable to type 'string;

Teeheeheeheehee that was fun.

Review

Omg I did really really well! I did REALLY well. I have finished the first lesson already!

"TypeScript expects that data type of the variable to match the type of the value initially assigned to it at its declaration - this is known as type inference".

"An object's shape describes, amongst other things, what properties and methods it does or doesn't contain. TypeScript knows not only what type something is but also what shape that type has [emphasis added]". 

"Type annotations are little pieces of code that indicate what type a variable is meant to be".

The Next Article Will Be...

"The tsconfig.json File"

Whooo! Sounds exciting.

Whooo!

Time Flies, Take Your Chance

Quoting "Come Along" now by Titiyo again as usual. "Time flies, take your chance." 

Don't hold back on anything you love.

Don't wait for anything you love.

Don't hold back.

Give it your all.

You might not get another chance to.



Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing