JS의 단점을 극복하기위해 나온 언어.
/**
* 1을 더한 값을 돌려주는 함수
* @param {number} value
* @returns {number} */
function addOne(value) {
return value + 1;
}
function addOne(value: number): number{
return value + 1;
}
// union
function sayAndReturnNumberOrString(phrase: number | string) {
console.log(`Hey, ${phrase}`);
return phrase;
}
interface IDBConfig {
host: string;
port: number;
user: string;
password: string;
schema?: string;
}
interface ExtraConfig {
encrypt: boolean;
dbName: string;
}
// intersection
// IDBConfig의 property와 ExtraConfig의 property를 합친 interface
function printDBConfig(config: IDBConfig & ExtraConfig) {
console.log(config);
}
type ID = string | number;
type Point = {
x: number;
y: number;
}
//
function point1(p: Point): Point{
return {x: p.x + 1, y: p.y + 1};
}
const p1: Point = {x: 2, y: 2}
console.log(point1({x: 1, y: 1}));
console.log(point1(p1));