개발/JavaScript
[Typescript] type 및 try-catch 시 return 처리
혜커 Hyeker
2024. 9. 13. 11:18
// psuedo code
class Person {
name: string;
foo() {
console.log(this.name);
}
constructor(name: string) {
this.name = name;
}
}
const p = new Person('kim');
console.log(JSON.stringify(p));
console.log(JSON.parse(JSON.stringify(p)));
function bar(): string {
let result = ''; // result가 try에서도 catch에서도 사용해야 할 경우 (부득이)
try {
result = 'success';
// 성공시 출구 1
return result;
} catch(e: any) {
// 성공시 출구 2-2
return result;
}
// 성공시 출구 2-1 (우선)
}