microsoft / microsoft/TypeScript

Add a `redeclare` keyword to redeclare class attributes, behaving like `override !:` while keeping type modifiers.

Open
#63,370 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔍 Search Terms

redeclare
declare override

Related issues:

✅ Viability Checklist
⭐ Suggestion

A redeclare keyword behaving like override + declare while keeping previous type modifiers (e.g. readonly, protected, public).

type BaseType  = {};
type Overriden = {x: number};

class A {
      protected readonly foo: BaseType;

      constructor(protected readonly faa: BaseType) {}
}

class B extends A {
    redeclare foo: Overriden;
    // equivalent to "override [protected readonly] foo!: Overriden"

    constructor( redeclare faa: Overriden ) { super(faa); }
    // idem above. Do not set faa itself.
}

We could use redeclare with modifiers, e.g. public or -readonly to override previous type modifiers:

redeclare public faa: Overriden; // make it public, do not change whether it is readonly or not, etc.
redeclare public faa;       // make it public, do not change its type, whether it is readonly or not, etc.
redeclare -readonly faa; // make it read-write, do not change its type or its visibility.

I also suggest the related changes:

  • in constructor parameters, !: would mean "declare but do not set".
  • using declare as an alias to !: when used in constructor parameters, or with override.

Note: redeclare could also be used in other contexts, e.g. to precise a variable type (cf related issue), but this is not in the scope of this suggestion.

📃 Motivating Example

Currently, if you want to override an attribute, you have to:

type BaseType  = {};
type Overriden = {x: number};

class A {
      protected readonly foo: BaseType;
}

class B extends A {
    override readonly foo!: Overriden;
}

However, if you forget one of the type modifier (here: protected) you would have no warnings as it might be voluntary. A reader do not know if this is a mistake or not.

By using redeclare you would ensure no type modifier is forgotten, while making intents explicit.
Indeed, if the type modifiers are modified in class A, using redeclare ensures that all subclasses has the correct type modifiers (except if explicitly stated otherwise).

Also, redeclare would enable to redeclare a type from the constructor, instead of having to provide the type twice.

type BaseType  = {};
type Overriden = {x: number};

class A {
      protected readonly foo: BaseType;
}

class B extends A {
    constructor( redeclare foo: Overriden ) { ... }
}
💻 Use Cases
  1. What do you want to use this for?

Safely re-declaring classes attributes.

  1. What shortcomings exist with current approaches?
  • We have to rewrite all type modifiers.
  • In constructors parameters, we would have useless (or even unwanted) attribute assignations.
  • Without constructors parameters, requires to provide the type twice.
  • With a generic base class, this is quite brittle : the base class and each of its subclasses needs to know which of the attribute might be redeclared. This could lead too an excessive amount of generic types if too much attributes.
  1. What workarounds are you using in the meantime?

I use constructors parameters with the useless assignations.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading related issues #51515, #37771, and #4062 alongside the redeclare examples and viability checklist. Clarify the proposed behavior for class attributes and constructor parameters, including inherited modifiers and explicit overrides; done would require an agreed language design, which this issue does not yet provide.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.