const message = "Hello World!";
// ...
message.toLowerCase(); // "hello world"
message(); // TypeError: message is not a function
function fn(x) {
return x.flip();
}
const message = "hello!";
message();
/*
This expression is not callable.
Type 'String' has no call signatures.
*/
const user = {
name: "Daniel",
age: 26,
};
user.location; // undefined
const user = {
name: "Daniel",
age: 26,
};
user.location;
// Property 'location' does not exist on type '{ name: string; age: number; }'.
// 오타
const announcement = "Hello World!";
// 의도한 코드: announcement.toLocaleLowerCase();
announcement.toLocaleLowercase();
/*
Property 'toLocaleLowercase' does not exist on type '"Hello World!"'. Did you mean 'toLocaleLowerCase'? */
// 호출되지 않은 함수
function flipCoin() {
// 의도한 코드: Math.random()
return Math.random < 0.5;
}
//Operator '<' cannot be applied to types '() => number' and 'number'.
//논리 오류
const value = Math.random() < 0.5 ? "a" : "b";
if (value !== "a") {
// ...
} else if (value === "b") {
}
/* This condition will always return 'false' since the types '"a"' and '"b"' have no overlap. */
hello.ts
//comment
console.log("Hello world!");
npm i typescript
// ts파일이 있는 폴더로 이동
tsc hello.ts
hello.js
//comment
console.log("Hello world!");
function greet(person, date) {
console.log(`Hello ${person}, today is ${date}!`);
}
greet("Brendan");
//terminal : Expected 2 arguments, but got 1.
tsc --noEmitOnError hello.ts
function greet(person: string, date: Date) {
console.log(`Hello ${person}, today is ${date.toDateString()}!`);
}
greet("Maddison", Date()); //Argument of type 'string' is not assignable to parameter of type 'Date'.
greet("Maddison", new Date()); // Date 객체를 전달하면 오류가 발생하지 않음
"use strict";
function greet(person, date) {
console.log("Hello ".concat(person, ", today is ").concat(date.toDateString(), "!"));
}
greet("Maddison", new Date());
--target
플래그를 사용해 컴파일할 버전 지정 가능출처:
https://www.typescriptlang.org/ko/docs/handbook/2/basic-types.html