question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion for readonly interface

See original GitHub issue

Avoid duplicating “readonly” for each property by applying it to the interface definition itself. This is to reduce boilerplate code defining immutable objects.

Having interface where ALL properties are read only:

interface State {
    readonly prop1: string;
    readonly prop2: string;
    ...
    readonly prop22: string;
}

equals to:

readonly interface State {
    prop1: string;
    prop2: string;
    ...
    prop22: string;
}

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:22
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
RomainMullercommented, Mar 13, 2019

I think there might be value in extending this to cover a C++ like const behavior…

const interface Foo {
  property: string; // Implicitly readonly
  method(): void; // Cannot mutate `this`
}

interface Bar {
  const method(): void; // Guaranteed non-mutating
  mutatingMethod(): void; // Could mutate `this`
}

function baz(obj: const Bar) { // Require "obj" to not be mutated by implementation
  obj.mutatingMethod(); // 💥 illegal call of mutating method on const reference
}

I don’t have particularly strong feelings on the const versus readonly term here… const is “nice” to me as it relates nicely to what this means in C++, which (some) people are used to.

2reactions
aleclarsoncommented, Mar 8, 2019

const interface might be nice for deep readonly (kinda like { a: { b: 1 } } as const)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recommended to use Readonly against Interfaces?
Recommended to use Readonly against Interfaces? · It's simply a check to ensure that once the object is fully constructed, that field cannot...
Read more >
Modifiable read-only Interface - CodeProject
Read-only interface is a simple thing. It does not allow the user to change its state. But sometimes, you may want to "change"...
Read more >
Are readonly interfaces possible? : r/typescript - Reddit
I would suggest against using readonly on anything but arrays. It's very half-baked and, honestly, will probably lead to more bugs by providing ......
Read more >
Making the interface form Read only!! - Appian Community
You can use parameter and make each form input read-only as suggested. Or you can create a second form with predefined read-only setting...
Read more >
c# - How would I design an interface such that it's clear which ...
This interface has two read-only properties, Id and IsInvalidated . The fact that they are read-only, however, is by itself no guarantee that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found