Skip to content
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

Merged
merged 9 commits into from
Sep 13, 2024

Conversation

lv-z-l
Copy link
Contributor

@lv-z-l lv-z-l commented Sep 3, 2024

behavior ts type support

@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 4, 2024

@SgLy @Mister-Hope 两位老哥,来review下这个pr

@SgLy
Copy link
Contributor

SgLy commented Sep 4, 2024

这种实现方式之前在 #230 中讨论过,它改变了 Behavior() 的返回值类型,变得和实际的类型不一样了,这点是比较难接受的。例如测试用例中的这种构造,会变成可以通过 TS 编译但不可能正确运行的:

// wrong use case
Component({
  behaviors: [{data: {}, properties: {}, methods: {}}],
})

这里是否有可能用更接近 string 的类型来携带 Behavior 需要的推导信息?我之前有过类似这样的思路,不过还没有深入尝试:

declare const virtualBehaviorKeySymbol: unique symbol
type BehaviorIdentifier<T> = string & { [virtualBehaviorKeySymbol]: SomeBehaviorInstanceContext<T> }

@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 4, 2024

确实,为了类型推导改变了实际的类型比较难接受,我再思考下,还有没有其他方案

@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 5, 2024

@SgLy 重新实现了,review下
image

@SgLy
Copy link
Contributor

SgLy commented Sep 6, 2024

Behavior 是能使用 Behavior 的,看看能不能实现类似下面这种情况的推导?可以补个用例

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
    }
  }
})

@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 6, 2024

Behavior 是能使用 Behavior 的,看看能不能实现类似下面这种情况的推导?可以补个用例

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
    }
  }
})

应该没啥问题,我抽空搞一下

@lv-z-l lv-z-l closed this Sep 9, 2024
@lv-z-l lv-z-l reopened this Sep 9, 2024
@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 9, 2024

@SgLy 支持了Behavior 中使用 Behavior 的类型推导,抽空来看下。57d4c82

@@ -70,13 +79,15 @@ declare namespace WechatMiniprogram.Component {
TData extends DataOption,
TProperty extends PropertyOption,
TMethod extends MethodOption,
TBehavior extends BehaviorOption = [],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个默认值是有必要的不,能不能和上面三个保持一致(都有默认值或者都没有)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(上面的 Options 也是)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

type IAnyArray = []
Copy link
Contributor

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 一起

Copy link
Contributor Author

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 & {
/** 组件数据,**包括内部数据和属性值** */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个注释被改没了,可以补回来一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

options: Options<TData, TProperty, TMethod, TCustomInstanceProperty>
): BehaviorIdentifier
options: Options<TData, TProperty, TMethod, TBehavior, TCustomInstanceProperty>
): BehaviorIdentifier & RealBehaviorType<TData, TProperty, TMethod, TBehavior>
Copy link
Contributor

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],
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@lv-z-l lv-z-l requested a review from SgLy September 12, 2024 02:46
@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 13, 2024

@SgLy 老哥,确认下评论,没问题 merge 下,我们还等着用呢。

@SgLy SgLy merged commit 6b8e1d3 into wechat-miniprogram:master Sep 13, 2024
1 check passed
@SgLy
Copy link
Contributor

SgLy commented Sep 13, 2024

这两个 PR 改了一些之前的公开接口,有些 breaking change,应该是要发一个 major 了

@lv-z-l
Copy link
Contributor Author

lv-z-l commented Sep 13, 2024

这两个 PR 改了一些之前的公开接口,有些 breaking change,应该是要发一个 major 了

嗯嗯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants