-
[Vue]record 문법 사용법Vue 2021. 1. 6. 15:11
type Operator = "+" | "-" | "*" | "/"; interface CalculateFunction { (operand1: number, operand2: number): number; } const CalcFunctions: Record<Operator, Function> = { "+": (a: number, b: number): number => a + b, "-": (a: number, b: number): number => a - b, "*": () => {}, "/": () => {}, }; function main() { const firstNum = 1; const secondNum = 2; const sumResult = CalcFunctions["+"](firstNum, secondNum); console.log(sumResult); } main()
switch case를 대신해서 쓸 수 있는 코드
'Vue' 카테고리의 다른 글
express async request handler 오류 (0) 2021.01.26 [Vue3+mongoDB+express ch.4] 라우터 설정 (router) (0) 2020.12.31 [Vue3+mongoDB+express ch.3] Frontend Backend 연결 (0) 2020.12.31 vue3 express mongoDB 개발환경 세팅 (0) 2020.12.29 typescript를 사용한 Vue3 프로젝트 환경 만들기 (0) 2020.12.24