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.

add a constraint for immutable types

See original GitHub issue

say, i have a generic function that by design requires that its parameters may only take immutable arguments, i wish i could get such guarantee from TS by declaring my function as follows:

function doThings<readonly T>(value: T): Result<T> {
   // ...
}

as far as how to make an interface immutable:

readonly interface Point {
   readonly x: number;
   readonly y: number;
}

so the problem being solve here is to make sure that the caller won’t mutate the argument after it is passed to the function

simple use case: a hash-based data container, which calculates a hash of a given value, and will store it at that hash, and it all will work until the value is mutated outside and tha stored hash is no longer valid, so the container doesn’t really work anymore

another use case: a cache or object pool or any other situation when there are many parties involved in taking hold of the same object, which must be guaranteed from being mutated by one party to prevent spooky action at a distance for all other parties

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zpdDG4gta8XKpMCdcommented, Mar 29, 2017

i was talking about special generic constraints that would guarantee that a type argument is immutable

what did you mean by Readonly?

interface Writable {
    value: string;
}
function willOnlyTakeReadonly<T>(data: Readonly<T>): void {
}
declare const writable: Writable ;
willOnlyTakeReadonly(writable); // <-- no problem

i am afraing it doesn’t work at least in the latest version of TS, because anything can be used in place of Readonly<T> parameter including interfaces with all writable properties

0reactions
mattmccutchencommented, Sep 26, 2017

I support the goals of this proposal. Some design issues:

  1. For the interface modifier that asserts that the object is immutable, overloading the readonly keyword is very confusing (it looks like it already confused mhegazy). Ideally we should use immutable, though I don’t know if there are concerns about adding new reserved words. (I’ll use immutable in the rest of my comment.)

  2. What prevents an interface containing the same members without the readonly modifier from being assigned structurally to the Point interface as declared above? Is the immutable modifier of a type one more thing that is checked for assignability?

  3. Users may want mutable, read-only, and immutable versions of the same interface. The current proposal for read-only (#10725 / #18770) is that the user defines a mutable interface Point and writes readonly Point for a deep read-only version (perhaps what “deep read-only” means has to be customized in some cases). Similarly, I’d like to write immutable Point for the deep-immutable interface. In essence, the immutable operator should declare fields that form part of the abstract state of the object to be immutable themselves; these are the same fields that become readonly along with the interface. The immutable interface has the same methods as the readonly interface. Given such an operator, the original example could be written as:

function doThings<T>(value: immutable T): Result<T> {
   // ...
}

but additionally having an immutable type parameter constraint may be clearer for users and/or make implementation easier depending on how type argument inference works (yikes).

  1. How do users produce immutable objects? One way is always a type assertion, or a deepFreeze method that uses reflection to find out what fields it should recursively deepFreeze and then asserts the result to be immutable. We could additionally consider ways of constructing a single new immutable object from existing immutable parts, e.g.:
immutable {a: 1, b: 2}
immutable [3, 4, 5]
immutable new MyPair(point1, point2)

To use the same constructor to construct both mutable and immutable objects, we need to make it polymorphic in whether the new object is mutable. I.e., we think of the presence or absence of the immutable modifier on the call as a generic parameter of the constructor, and throughout the constructor’s signature and body, the owned type modifier refers to this parameter. Then the compiler checks every assignment to a field to ensure that if the new object is immutable, then the reference being assigned is immutable. (Is anyone aware of precedent for this in other programming languages? I could research it myself, but I’ve spent enough time on this already.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating deep immutable types - Learn TypeScript
Creating deep immutable types. In this lesson, we will learn various ways of making types deeply immutable. Using const assertions.
Read more >
Set Immutable constraints - Python - Java2s.com
Sets can only contain immutable object types. Hence, lists and dictionaries cannot be embedded in sets, but tuples can if you need to...
Read more >
c# - How to Ensure Immutability of a Generic - Stack Overflow
How to Ensure Immutability of a Generic · 1. struct -only. Add where T : struct to your Datum<T> class. · 2. Create...
Read more >
An Immutability Type System for Classes and Objects
Annotator classes are used to put a special qualifier into the code: @VarAnnot, which contains the ID of the variable in the solver....
Read more >
Mutable and Immutable Objects - Manning
Objects of these types can't be modified once created. Suppose you have the following lines of code, which are executed in the order...
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