
function makePerson(name, age) { }
makePerson("soo", "20")

function makePerson(name:string, age:number) { }
makePerson("soo", "20")


$ npm install -g typescript
$ tsc --version
$ npx create-react-app type-app --template typescript

function App() {
return <div></div>;
}
export default App;
function App() {
const name = "TypeScript";
return <h1>{name}</h1>;
}
export default App;
$ npm start
| 유형 | 자바스크립트 타입 | 타입스크립트 타입 |
|---|---|---|
| 숫자 타입 | Number | number |
| 불리언 타입 | Boolean | boolean |
| 문자열 타입 | String | string |
| 객체 타입 | Object | object |
let 변수이름
let 변수이름 = 초기값
const 변수이름 = 초기값
let 변수이름: 타입
let 변수이름: 타입 = 초기값
const 변수이름: 타입 = 초기값
let a: number = 1
let b: string = "hello"
let c: boolean = true
let d: object = {}
대입 연산자(=)의 오른쪽 값을 분석해 왼쪽 변수의 타입을 결정
이후 각 변수에는 해당 타입의 값만 저장 가능
let a = 1 // a의 타입을 number로 판단
let b = "hello" // b의 타입을 string으로 판단
let c = true // c의 타입을 boolean으로 판단
let d = {} // d의 타입을 object로 판단
let a: any = 0
a = "hello"
a = true
a = {}
let a: string | number;
a = "10"; // string 값 저장
a = 20; //number 값 저장
enum GenderType {
Male,
Female
}
type 새로운타입 = 기존타입
`${변수이름}`
let name: string = "Sooa";
let result = `My name is ${name}`;
console.log(result);
function 함수이름(매개변수1: 타입1, 매개변수2: 타입2): 반환값_타입 {
// 작업
}
function add(a: number, b: number): number {
return a + b;
}
let result = add(1, 2);
console.log(result);
function add(a: string, b: string): string {
return a + b;
}
let result = add("Hello", "SOOA");
console.log(result);
function add(a: string, b: string): string[] {
return [a, b];
}
let result = add("Hello", "SOOA");
console.log(result);
function add(a: string, b: string): void {
console.log(a, b);
}
add("Hello", "SOOA");
function printValue(a: number, b: string): void {
console.log(`a : ${a}, b : ${b}`)
}
function add(a: string, b: string): void {
console.log(a, b);
}
// add()에 매개변수를 한 개만 전달
add("Hello"); // 에러 발생
function add(a: string, b**?**: string): void {
console.log(a, b);
}
// add()에 매개변수를 한 개만 전달
add("Hello"); // 정상 실행

function 함수이름(매개변수: 타입 = 기본값): 반환값_타입 {
// 작업
}