타입스크립트 함수 타입 사용 방법
-
function add(a, b) {
return a + b;
}-
function add(a: number, b: number): number {
return a + b;
}-
a: number // a는 숫자
b: number // b는 숫자
: number // 함수의 반환값도 숫자-
(매개변수: 타입) => 반환타입-
(name: string) => void-
(a: number, b: number) => number-
function greet(name: string): string {
return `안녕하세요, ${name}님`;
}
const message = greet("철수");
console.log(message);-
name: string-
: string-
greet(123);-
function printLog(message: string): void {
console.log(message);
}-
printLog("로그를 출력합니다.");-
const add = (a: number, b: number): number => {
return a + b;
};-
const add = (a: number, b: number): number => a + b;-
const add: (a: number, b: number) => number = (a, b) => {
return a + b;
};-
type AddFunction = (a: number, b: number) => number;
const add: AddFunction = (a, b) => {
return a + b;
};-
const multiply: AddFunction = (a, b) => {
return a * b;
};-
function calculate(
a: number,
b: number,
callback: (x: number, y: number) => number
): number {
return callback(a, b);
}
const result = calculate(10, 5, (x, y) => x + y);
console.log(result);-
callback: (x: number, y: number) => number-
type CalculateCallback = (x: number, y: number) => number;
function calculate(
a: number,
b: number,
callback: CalculateCallback
): number {
return callback(a, b);
}-
function introduce(name: string, age?: number): string {
if (age) {
return `${name}님은 ${age}살입니다.`;
}
return `${name}님입니다.`;
}-
introduce("민수");
introduce("지영", 25);-
function doubleAge(age?: number): number {
if (age === undefined) {
return 0;
}
return age * 2;
}-
function createMessage(name: string, greeting: string = "안녕하세요"): string {
return `${greeting}, ${name}님`;
}
createMessage("철수");
createMessage("영희", "반갑습니다");-
function hello(name?: string) {
// name이 undefined일 수 있음
}
function hi(name: string = "사용자") {
// name에 기본값이 있음
}-
type User = {
name: string;
age: number;
};
function printUser(user: User): void {
console.log(`${user.name}님은 ${user.age}살입니다.`);
}
printUser({
name: "수진",
age: 30,
});-
function printUser(user: { name: string; age: number }): void {
console.log(user.name);
}-
function getNames(): string[] {
return ["철수", "영희", "민수"];
}-
function getScores(): number[] {
return [90, 80, 100];
}-
type Product = {
id: number;
name: string;
};
function getProducts(): Product[] {
return [
{ id: 1, name: "키보드" },
{ id: 2, name: "마우스" },
];
}-
async function fetchUserName(): Promise<string> {
return "철수";
}-
type User = {
id: number;
name: string;
};
async function fetchUser(): Promise<User> {
return {
id: 1,
name: "지민",
};
}-
type ApiResponse = {
success: boolean;
message: string;
};
async function submitForm(): Promise<ApiResponse> {
return {
success: true,
message: "저장되었습니다.",
};
}-
function identity<T>(value: T): T {
return value;
}
const text = identity<string>("hello");
const count = identity<number>(10);-
const text = identity("hello");
const count = identity(10);-
function getFirstItem<T>(items: T[]): T {
return items[0];
}
const firstName = getFirstItem(["철수", "영희"]);
const firstNumber = getFirstItem([1, 2, 3]);-
댓글
댓글 쓰기