A better way to use mixins ?
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 38/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- typescript
- Lĩnh vực
- documentation
Hướng nghiên cứu
Bắt đầu với trang mixins của TypeScript Handbook được liên kết trong issue và xem xét ví dụ Playground được cung cấp. Đánh giá cách mẫu được đề xuất và các giới hạn đã nêu của nó phù hợp với tài liệu hiện có, sau đó cập nhật hướng dẫn về mixins bằng một giải pháp thay thế chính xác và các lưu ý rõ ràng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
The document gives some examples on how to use mixin here: https://www.typescriptlang.org/docs/handbook/mixins.html
However the 2 proposed solutions has issues:
- the first one prevent us from having private/protected members, and may generate quite a lot of type deduction error in the function.
- the second is not that great regarding to type safety.
I think we have a better solution to use mixin, that could be added to the documentation:
class Base {}
// declare the added properties outside of the mixin function
abstract class Mixin {}
class K extends mix(Base, Mixin) { }
Such generic function could be implemented like this:
const MixSrc = Symbol();
type Mix<
Base extends new(...args:any[])=>any,
Mixin extends abstract new(...args:any[])=>any,
> = Omit<Base & Mixin, "new"> & (new(...args:ConstructorParameters<Base>) => (InstanceType<Base> & InstanceType<Mixin>))
function mix<
Base extends new(...args:any[]) => any,
Mixin extends abstract new(...args:any[]) => any
>(base: Base, mixin: Mixin): Mix<Base, Mixin> {
class _ extends base {}
const static_props = Object.getOwnPropertyDescriptors(mixin);
delete static_props.prototype;
delete static_props.name;
Object.defineProperties( _, static_props );
((_ as any)[MixSrc] ??= []).push(mixin);
const hasInstance = mixin[Symbol.hasInstance];
Object.defineProperty(mixin, Symbol.hasInstance, {
value: function (instance: any) {
if( instance.constructor[MixSrc].includes(this) )
return true;
return hasInstance.call(this, instance);
},
writable: false,
});
const instance_props = Object.getOwnPropertyDescriptors(mixin.prototype);
// @ts-ignore
delete instance_props.constructor;
Object.defineProperties( _.prototype, instance_props );
return _ as any; // well...
}
There are 2 limitations of this method:
- This may not work if
Mixininherit from another class ( though you could just callmixfor each of theMixinbases ). - This will not work if
Mixinhas private#properties.
If we have private properties, we can do something like:
abstract class Mixin {
// declare the public/protected interface here
}
type ExcludeProtected<T> = {[K in keyof T]: T[K]}
function addMixin<...>(base): Mix<Base, Mixin> {
return class _ extends base implements ExcludeProtected<Mixin> {
// implements Mixin methods here...
// can't guarantee the protected interface (due to TS limitations).
} as any;
}
But then Mixin must not inherit from another class.
- Ngôn ngữ chính
- TypeScript
- Star
- 2.6k
- Fork
- 1.5k
- Merge trung bình
- 2 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 7
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của microsoft/TypeScript-Website
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
microsoft/TypeScript-Website#3611 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
microsoft/TypeScript-Website#3607 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
microsoft/TypeScript-Website#3039 ·
-
More examples of `infer` Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
microsoft/TypeScript-Website#2998 ·
-
@typescript/vfs emits an ExperimentalWarning on Node 26 during localStorage feature detection Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 30/100
microsoft/TypeScript-Website#3650 ·
Tất cả issue của microsoft/TypeScript-Website
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
area:tools bug good first issue help wanted priority:P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
TaewoooPark/Motifcode#14 ·
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
newrelic-experimental/preflight#793 · 1 bình luận ·
-
bug 🐞
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
[Bounty proposal] fix(web): memory insights count an evening memory on the next day ($25 proposed) Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
BasedHardware/omi#15320 ·