Skip to content

Are there some good approach to remove 'readonly' restriction templorary from fields? #6717

Closed
@tetsuharuohzeki

Description

@tetsuharuohzeki

environment

  • typescript@1.9.0-dev.20160128

description

relate? : #6532

In some case (e.g. to development framework), I'd like to do a destruct operation for the members which is restricted as readonly to release a some large object explicitly. But it would be the compile error if I assign a value to the restricted member in a non-constructor() method.

class A {
  readonly a: SomeLargeObject;
  constructor(a: SomeLargeObject) {
    this.a = a;
  }

  // explicit destructor
  destroy(): void {
    // release this some large object explicitly
    this.a = null; // <-- compile error
    (<any>this).a = null; // <-- no problem
    (<MutableA>this).a = null // <--- no problem
  }
}

interface MutableA {
  a: SomeLargeObject;
}

I know we can avoid this error if casting this to any type, but it would not be a type safe. In other approach, we can also do it if we define a "mutable" interface and cast to it, but it would be wordy....

Are there some good approach to remove 'readonly' restriction templorary?

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions