그냥 기본.
let 이름: string = "kim";
let 나이: number = 50;
let 결혼했니: boolean = false;
let 결혼했니2: undefined = undefined;
let 회원들: string[] = ["kim", "park"];
let 회원들2: { name: string; age: number } = { name: "kim", age: 30 };
let project: { member: string[]; days: number; started: boolean } = {
member: ["kim", "park"],
days: 30,
started: true,
};
유니온
let kakouser: number | string = "kim";
kakouser = 123;
let users: (number | string)[] = [1, "2", 3];
let 오브젝트: { a: string | number } = { a: "123" };
any, unknown
let username: any; //이럴거면 typescript 기능을 없애는것.
username = 123;
username = [];
let username2: unknown;
username2 = 123;
username2 = [1, 2, 3];
function nameWrite(x?: string) {
if (typeof x === "string") {
console.log("안녕하세요" + x);
} else {
console.log("이름이 없습니다");
}
}
function parameterLength(x: string | number) {
console.log(x.toString().length);
}
unction 결혼가능섣테스트(
월소득: number,
집보유: boolean,
매력점수: string
): string | void {
if (집보유) {
var havHouseScore = 500;
}
if (매력점수 === "상") {
var attactionScore = 100;
}
if (월소득 + havHouseScore + attactionScore > 600) {
return "결혼가능";
}
}
console.log(결혼가능섣테스트(700, false, "중"));