본문 바로가기
반응형

Interface3

Property 'done' does not exist on type 'object' .ts 타입 정의 하기 function completeTodo(index:number, todo:{ id: number, title: string, done: boolean, }): void { todo.done = true; todoItems.splice(index, 1, todo); } 타입 정의 let todoItems: { id: number, title: string, done: boolean }[]; function completeTodo(index:number, todo:{ id: number, title: string, done: boolean, }): void { todo.done = true; todoItems.splice(index, 1, todo); } 중복되는 코드를 줄이고 타입을 하나로.. 2023. 12. 24.
타입스크립트 interface 아래 예문에서 함수는 object[]를 타입이다. 매개변수도 타입을 정의해줬는데, 함수 내부에서 파라미터의 속성의 타입이 없다는 에러를 발견했을 때의 오류는 어떻게 해결해야 할까? parameter의 타입이 object일 때 let todoItems: object[]; function completeTodo(index:number, todo:object):void { todo.done = true; todoItems.splice(index, 1, todo); } todo.done의 부분 done이 빨간 밑줄이 생기는데 속성의 타입이 없다고 한다. 이럴때는 객체의 형태를 구체적으로 타입을 정의해줘야 한다. let todoItems: {{id:number, title: string, done: boolean.. 2022. 12. 4.
타입스크립트_기본타입, interface Typescript function showTimes(arr) { arr.forEach(element => { console.log(element) }); } showTimes([1,2,3]) //1, 2, 3 showTimes(1,2,3) //error 발생 Javascript(동적언어): 실행되는 시점 (런타임)에 타입이 결정되고 오류가 있을 시 그때 처리가 가능하다. function showTimes(arr:number[]) { arr.forEach(element => { console.log(element) }); } showTimes([1,2,3]) //1, 2, 3 function showTimes(arr:string[]) { arr.forEach(element => { console.log(.. 2022. 11. 30.
728x90
반응형