Make URLRouter in options conditional on generic
URLRouter can be passed in option to vm only if the SegmentType used contains session. ViewModel.urlCreator returns undefined when used with a SegmentType that lacks session.
This commit is contained in:
parent
a336623f3a
commit
ec1cc89cf9
1 changed files with 10 additions and 6 deletions
|
@ -30,15 +30,17 @@ import type {Navigation} from "./navigation/Navigation";
|
||||||
import type {SegmentType} from "./navigation/index";
|
import type {SegmentType} from "./navigation/index";
|
||||||
import type {URLRouter} from "./navigation/URLRouter";
|
import type {URLRouter} from "./navigation/URLRouter";
|
||||||
|
|
||||||
type Options<N extends {session: string}> = {
|
type OptionsWithoutUrlCreator<N extends object> = {
|
||||||
platform: Platform
|
platform: Platform
|
||||||
logger: ILogger
|
logger: ILogger
|
||||||
urlCreator: URLRouter<N>
|
|
||||||
navigation: Navigation<N>
|
navigation: Navigation<N>
|
||||||
emitChange?: (params: any) => void
|
emitChange?: (params: any) => void
|
||||||
}
|
}
|
||||||
|
type OptionsWithUrlCreator<N extends { session: string }> = OptionsWithoutUrlCreator<N> & {urlCreator: URLRouter<N>};
|
||||||
|
|
||||||
export class ViewModel<N extends {session: string} = SegmentType, O extends Options<N> = Options<N>> extends EventEmitter<{change: never}> {
|
type Options<N extends object> = N extends { session: string } ? OptionsWithUrlCreator<N> : OptionsWithoutUrlCreator<N>;
|
||||||
|
|
||||||
|
export class ViewModel<N extends object = SegmentType, O extends Options<N> = Options<N>> extends EventEmitter<{change: never}> {
|
||||||
private disposables?: Disposables;
|
private disposables?: Disposables;
|
||||||
private _isDisposed = false;
|
private _isDisposed = false;
|
||||||
private _options: Readonly<O>;
|
private _options: Readonly<O>;
|
||||||
|
@ -136,11 +138,13 @@ export class ViewModel<N extends {session: string} = SegmentType, O extends Opti
|
||||||
return this.platform.logger;
|
return this.platform.logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
get urlCreator(): URLRouter<N> {
|
get urlCreator(): N extends { session: string }? URLRouter<N>: undefined {
|
||||||
return this._options.urlCreator;
|
// typescript needs a little help here
|
||||||
|
return (this._options as unknown as {urlCreator: any}).urlCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
get navigation(): Navigation<N> {
|
get navigation(): Navigation<N> {
|
||||||
return this._options.navigation;
|
// typescript needs a little help here
|
||||||
|
return this._options.navigation as unknown as Navigation<N>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue