CyanSalt / notebook

3 stars 0 forks source link

在 TypeScript 中实现类似于 Vue Component Options 的类型检查 #7

Open CyanSalt opened 6 years ago

CyanSalt commented 6 years ago

path: ts-check-vue-component-options


interface Store<States, Actions> {
  states?: States
  actions?: Actions & ThisType<States & Readonly<Actions>>
}

function typed<States, Actions>(
  store: Store<States, Actions>
): Store<States, Actions> {
  return store
}

typed({
  states: {
    a: 1,
  },
  actions: {
    t() {
      this.a = 2
      // Type error here when `--noImplicitThis` enabled
      this.t = () => { }
    }
  }
})