변수 타입
- Boolean
- Number
- String
- Object
- Array
- Tuple
- Enum
- Any
- Void
- Null
- Undefined
- Never
변수 문법
var str = 'hello';
var str: string = 'hello';
var numbers: number[] = [1, 2, 3];
var arr: Array<string> = ['hello', 'world'];
var address: [string, number] = ['seoul', 10];
var obj: object = {
};
var person: {name: string, age: number} = {
name: 'kim',
age: 10
}
함수 문법
function sum1(a: number, b: number): number {
return a + b;
}
function sum2(a: number, b?: number = 0): number {
return a + b;
}
인터페이스 문법
interface User {
name: string;
age: number;
}
var kim: User = {
name: 'kim',
}
interface SumFunc {
(a: number, b: number): number;
}
var sum1: SumFunc = function (a: number, b: number): number {
return a + b;
};
var sum2: SumFunc = (a: number, b: number): number => {
return a + b;
};
interface StringArray {
[index: number]: string;
}
var stringArr: StringArray = ['a', 'b', 'c'];
arr[0] = 10;
interface RegExpArray {
[key: string]: RegExp;
}
var regExpArr: RegExpArray = {
cssFile: '*.css',
jsFile: /\.js$/,
};
regExpArr['javaFile'] = /\.java$/
interface Person {
name: string;
age: number;
}
interface Developer extends Person {
language: string;
}
var kim: Developer = {
language: 'Typescript',
};
파라미터 문법
interface Developer {
name: string;
skill: string;
}
interface Person {
name: string;
age: number;
}
function someFunction1(worker: Developer | Person) {
worker.name;
worker.age;
}
function someFunction2(worker: Developer & Person) {
worker.name;
worker.age;
worker.skill;
}
클래스 문법
function Person(name, age) {
this.name = name;
this.age = age;
}
const kim = new Person('kim', 10);
class Person {
name: string;
age: number;
constructor() {
this.name = ''
this.age = 0
}
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
const kim = new Person('kim', 10);
var user = {name: 'kim', age: 10};
var admin = {};
admin.__proto__ = user;