// TypeScript에서 객체의 키와 값이 동적으로 설정될 수 있도록 문법
// index signiture 를 이용
class ExerciseDataDTO {
[bodyPart: string]: number[];
}
class YearlyDataDTO {
[year: number]: ExerciseDataDTO;
}
export class AggregatedResultDTO {
year: YearlyDataDTO;
constructor() {
this.year = {};
}
}
const helloWorld = "Hello World";
helloWorld = "Hi world"; // Cannot assign to 'helloWorld' because it is a constant.
Object.freez() 이용
const myDefinitelyConstantObject = Object.freeze({
msg: "Hello World",
});
// Cannot assign to 'msg' because it is a read-only property.
myDefinitelyConstantObject.msg = "Hi"
const myFrozenArray = Object.freeze(["Hi"]);
//Property 'push' does not exist on type 'readonly string[]'.
myFrozenArray.push("World");
readonly 이용
class UserDetails {
readonly name: string
readonly city: string
readonly phoneNo: number
constructor(name: string, city: string, phoneNo: number){
this.name = name
this.city = city
this.phoneNo = phoneNo
}
}
const user1 = new UserDetails("ABCD", "XYZ", 1234567890);
const user2 = new UserDetails("APPLE", "PQR", 1269785100);