Is there a way to initialize an object literal and declare its interface with read-only property in-hand at the same time ?
For example
let a = { readonly b: 2, readonly c: 3 }
You can use a as const assertion:
let a = { b: 2, c: 3 } as const // typed as { readonly b: 2; readonly c: 3; }
a.b = 2 //Cannot assign to 'b' because it is a read-only property.
If you want only some props to be readonly, that is not really possible, best you can do is use an Object.assign with one part containing readonly properties and the other containing the mutable properties:
let a = Object.assign({ b: 2, c: 3 } as const, {
d: 0
});
a.b = 2 // err
a.d = 1 //ok
"as const" is just a compile time error, not runtime error, have to use getter only property.
const a = Object.assign({ get b() { return 2 }, get c() { return 3 } }, {
d: 0
});
a.b = 2 // err
a.d = 1 //ok
alert(JSON.stringify(a))
FYI, Object.Freeze make whole object can't asssign new propeties
TS Playground, see transformed code, and run it