Typescript. What’s for?

Example №1

in js we can do tricks like this

const message = "hi";

message.toUpperCase(); // keeping in mind that it is a string
/*
...
work..
...decide other task
*/

message(); // ups... forgot that message is string

now in browser we will see this error only in browser

TypeError: message is not a function

using typescript we can see errors on stage of compile

tsc index.ts

index.ts:2:1 - error TS2349: This expression is not callable.

Example №2

in js we can just add props to object

const u = {
    name: "Daniel",
    family: "Mac"
}

console.log(u.age); // just will give us undefined without throwing en error

in typescript we will receive smth like this
Property 'age' does not exist on type '{ name: string; age: number; }'.

Example №3

// uncallable functions

const x = Math.random < 0.5;
Operator '<' cannot be applied to types '() => number' and 'number'.

Also code completion and error checking

This entry was posted in Без рубрики. Bookmark the permalink.