10000 add a constraint for immutable types · Issue #14909 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content
add a constraint for immutable types #14909
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0