microsoft / microsoft/TypeScript

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

Đang mở
#63,370 2 bình luận 2 reaction 0 người được giao Xem trên GitHub
Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

### 🔍 Search Terms

redeclare
declare override

Related issues:
- https://github.com/microsoft/typescript/issues/51515
- https://github.com/microsoft/typescript/issues/37771
- https://github.com/microsoft/TypeScript/issues/4062

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

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

```ts
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:
```ts
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:
```ts
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.
```ts
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.

3. 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.

4. What workarounds are you using in the meantime?

I use constructors parameters with the useless assignations.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng việc đọc các issue liên quan #51515, #37771 và #4062 cùng với các ví dụ về khai báo lại và checklist về tính khả thi. Làm rõ hành vi được đề xuất cho các thuộc tính của lớp và các tham số của constructor, bao gồm các modifier được kế thừa và các ghi đè tường minh; để được coi là hoàn tất sẽ cần có một thiết kế ngôn ngữ đã được thống nhất, điều mà issue này hiện chưa cung cấp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
typescript
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.