🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsHeader and Footer not showing in Excel(14.09.2026 um 22:43 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsHeader and Footer not showing in Excel(14.09.2026 um 22:43 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 2 Min Lesezeit
0

Cautions When Using readonly in TypeScript

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht




The basic of readonly property



In Type Script, you can make the object of the properties of an object read-only.




CODE
const person: { readonly name: string  } = { name: 'Mike' }

person.name = 21;
// → Cannot assign to 'name' because it is a read-only property.












⚠️① readonly is only at compile-time



In the compiled JavaScript code, the readonly declaration is removed, so it will not be detected as an error at runtime.






⚠️② readonly is not recursive.






CODE
const person: {
readonly name: string;
readonly academicBackground: {
primarySchool: string
}
} = {
name: 'Mike',
academicBackground: {
primarySchool: 'School A'
}
}

person.academicBackground.primarySchool = 'School B'
// You can change `person.academicBackground.primarySchool`






If you want to make it read-only, also you need to put readonly to primarySchool.




CODE
const person: {
readonly name: string;
readonly academicBackground: {
readonly primarySchool: string
}
} = {
name: 'Mike',
academicBackground: {
primarySchool: 'School A'
}
}

person.academicBackground.primarySchool = 'School B'
// → Cannot assign to 'primarySchool' because it is a read-only property.












Readonly



When the number of properties increases, adding readonly to each one becomes cumbersome and increases the amount of code.

You can refactor by using Readonly.




CODE
const obj: {
readonly a : string;
readonly b: string;
readonly c: string;
readonly d: string;
} = {
a: 'a',
b: 'b',
c: 'c',
d: 'd'
}

// ↓

const obj: Readonly<{
a : string;
b: string;
c: string;
d: string;
}> = {
a: 'a',
b: 'b',
c: 'c',
d: 'd'
}






Happy Coding☀️

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Header and Footer not showing in Excel
1 Quelle
Burn Out, Or Fade Away
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Cautions When Using readonly in TypeScript

Thematisch verwandte Begriffe: Cautions, When, Using, readonly · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...