Skip to content

Commit c5be226

Browse files
authored
feat: inheritance should work with computed (#3950)
* inheritance should work with computed * fix * improve
1 parent 53bf33c commit c5be226

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/mobx/__tests__/decorators_20223/stage3-decorators.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,3 +1115,22 @@ test(`decorated field can be inherited, but doesn't inherite the effect of decor
11151115
const subStore = new SubStore()
11161116
expect(isAction(subStore.action)).toBe(false)
11171117
})
1118+
1119+
test(`inheritance should work with computed`, () => {
1120+
class Store {
1121+
@computed
1122+
get getNumber() {
1123+
return 1
1124+
}
1125+
}
1126+
1127+
class SubStore extends Store {
1128+
@computed
1129+
override get getNumber() {
1130+
return super.getNumber + 1
1131+
}
1132+
}
1133+
1134+
const store = new SubStore()
1135+
expect(store.getNumber).toBe(2)
1136+
})

packages/mobx/src/types/computedannotation.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ function decorate_20223_(this: Annotation, get, context: ClassGetterDecoratorCon
6464
options.name ||= __DEV__
6565
? `${adm.name_}.${key.toString()}`
6666
: `ObservableObject.${key.toString()}`
67-
adm.values_.set(key, new ComputedValue(options))
67+
const computedValue = new ComputedValue(options)
68+
adm.values_.set(key, computedValue)
69+
adm.values_.set(get, computedValue)
6870
})
6971

7072
return function () {
71-
return this[$mobx].getObservablePropValue_(key)
73+
return this[$mobx].getObservablePropValue_(get)
7274
}
7375
}
7476

0 commit comments

Comments
 (0)