【发布时间】:2021-09-04 11:15:39
【问题描述】:
interface ReadExample {
readonly book_id: number,
book_name: string
}
class Book implements ReadExample{
book_id= 3;
book_name = 'A Thousand Stars';
constructor(book_id:number, book_name:string){
this.book_id = book_id;
this.book_name = book_name;
}
}
let book: Book = new Book(2, 'Sis');
console.log(book);
book.book_name = 'Sister';
book.book_id = 3;
console.log(book);
为什么这不会给我带来任何错误。您会看到属性 book_id 是只读的。那么为什么当我尝试分配 book.book_id = 3 时它不会在这里抛出错误?不违反只读吗?
【问题讨论】:
标签: typescript interface readonly