You can still use the old method if you want. To make the typescript error go away, both during declaration and later during use you can create a definition override for the model, something like here:
https://github.com/clarkwinkelmann/flarum-ext-who-read/blob/master/js/shims.d.ts
import UserState from './src/forum/models/UserState';
declare module 'flarum/common/models/Discussion' {
export default interface Discussion {
clarkwinkelmannWhoReaders(): (UserState | undefined)[] | false
}
}
That example is for a hasMany()
relation. For a hasOne
the return type would just be relationName(): RelationModel | false
I have other such examples across the Flamarkt extensions, like https://github.com/flamarkt/library/blob/main/js/shims.d.ts
declare module 'flamarkt/core/common/models/Product' {
export default interface Product {
thumbnail: () => File | false
}
}
The shims file can then be registered in tsconfig.json
: (https://github.com/clarkwinkelmann/flarum-ext-who-read/blob/master/js/tsconfig.json)
"files": [
"shims.d.ts"
],
I have been using that method in many extensions. The only downside/issue I found is that I don't know how to import the declaration when another extension extends the first extension. But that's quite a specific use case.