-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: behavior ts type support #333
Conversation
@SgLy @Mister-Hope 两位老哥,来review下这个pr |
这种实现方式之前在 #230 中讨论过,它改变了 // wrong use case
Component({
behaviors: [{data: {}, properties: {}, methods: {}}],
}) 这里是否有可能用更接近 declare const virtualBehaviorKeySymbol: unique symbol
type BehaviorIdentifier<T> = string & { [virtualBehaviorKeySymbol]: SomeBehaviorInstanceContext<T> } |
确实,为了类型推导改变了实际的类型比较难接受,我再思考下,还有没有其他方案 |
@SgLy 重新实现了,review下 |
const behaviorA = Behavior({
data: {
A: 1,
}
})
const behaviorB = Behavior({
behaviors: [behaviorA],
data: {
B: 2
}
})
Component({
behaviors: [behaviorB],
methods: {
fn() {
console.log(this.data.A) // expected 1
console.log(this.data.B) // expected 2
}
}
}) |
应该没啥问题,我抽空搞一下 |
types/wx/lib.wx.component.d.ts
Outdated
@@ -70,13 +79,15 @@ declare namespace WechatMiniprogram.Component { | |||
TData extends DataOption, | |||
TProperty extends PropertyOption, | |||
TMethod extends MethodOption, | |||
TBehavior extends BehaviorOption = [], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个默认值是有必要的不,能不能和上面三个保持一致(都有默认值或者都没有)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(上面的 Options
也是)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
types/wx/lib.wx.component.d.ts
Outdated
} | ||
|
||
type IAnyArray = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个或许应该是 IEmptyArray
好一点?(和 any[]
区别)
可以挪到 index.d.ts
里面和 IAnyObject
一起
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
(TIsPage extends true ? Page.ILifetime : {}) & | ||
TCustomInstanceProperty & { | ||
/** 组件数据,**包括内部数据和属性值** */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个注释被改没了,可以补回来一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
types/wx/lib.wx.behavior.d.ts
Outdated
options: Options<TData, TProperty, TMethod, TCustomInstanceProperty> | ||
): BehaviorIdentifier | ||
options: Options<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty> | ||
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod, TBehavior> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 RealBehaviorType
可能还是直接交叉进 BehaviorIdentifier
好一点,比较方便外面引用,比如
const behaviorA: BehaviorIdentifier = Behavior()
或者
const myBehaviors: Record<string, BehaviorIdentifier> = {}
Component({
behaviors: [myBehaviors.computed],
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@SgLy 老哥,确认下评论,没问题 merge 下,我们还等着用呢。 |
这两个 PR 改了一些之前的公开接口,有些 breaking change,应该是要发一个 major 了 |
嗯嗯 |
behavior ts type support