Skip to content

Commit e69e83f

Browse files
author
Jan Ackermann
committed
feat: Further improve typing
1 parent a70867f commit e69e83f

File tree

5 files changed

+189
-28
lines changed

5 files changed

+189
-28
lines changed

package-lock.json

Lines changed: 155 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414
},
1515
"scripts": {
16-
"build": "rimraf dist && rollup -c",
16+
"build": "rimraf dist && rollup -c && tsc-alias",
1717
"build:watch": "rollup -cw",
1818
"test": "cross-env BABEL_ENV=test vitest run --coverage",
1919
"format": "prettier --write .",
@@ -68,6 +68,7 @@
6868
"rimraf": "^6.0.1",
6969
"rollup": "^4.20.0",
7070
"single-spa": "^6.0.1",
71+
"tsc-alias": "^1.8.10",
7172
"typescript": "^5.5.4",
7273
"typescript-eslint": "^8.0.1",
7374
"vitest": "^2.0.5",

src/single-spa-vue.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Component, ComponentOptions, h as H } from "vue-demi";
2-
import type {
2+
import {
33
SingleSpaVueOpts,
44
SingleSpaOptsVue2,
55
SingleSpaOptsVue3,
@@ -10,11 +10,12 @@ import type {
1010
InstanceVue2,
1111
InstanceVue3,
1212
VueLifecycles,
13+
Props,
1314
} from "@/types";
14-
import type { AppProps, ParcelProps } from "single-spa";
15+
import { AppProps } from "single-spa";
1516

1617
export default function singleSpaVue<ExtraProps>(
17-
opts: SingleSpaVueOpts,
18+
opts: SingleSpaVueOpts<ExtraProps>,
1819
): VueLifecycles<ExtraProps> {
1920
const isVue2 = (opts: SingleSpaVueOpts): opts is SingleSpaOptsVue2 => {
2021
return (opts as SingleSpaOptsVue2).Vue !== undefined;
@@ -53,8 +54,8 @@ export default function singleSpaVue<ExtraProps>(
5354
opts.createApp || (opts.Vue && opts.Vue.createApp);
5455

5556
const resolveAppOptions = async (
56-
opts: SingleSpaVueOpts,
57-
props: ExtraProps & AppProps,
57+
opts: SingleSpaVueOpts<ExtraProps & Props>,
58+
props: ExtraProps & Props,
5859
): Promise<AppOptionsObject> => {
5960
if (typeof opts.appOptions === "function") {
6061
return (opts.appOptions as AppOptionsFunction)(opts, props);
@@ -64,9 +65,9 @@ export default function singleSpaVue<ExtraProps>(
6465
};
6566

6667
const mount = async (
67-
opts: SingleSpaVueOpts,
68+
opts: SingleSpaVueOpts<ExtraProps & Props>,
6869
mountedInstances: Record<string, Instance>,
69-
props: ExtraProps & AppProps & ParcelProps,
70+
props: ExtraProps & Props,
7071
) => {
7172
await Promise.resolve();
7273
const instance: Instance = {};
@@ -172,7 +173,7 @@ export default function singleSpaVue<ExtraProps>(
172173
}
173174
};
174175

175-
const bootstrap = async (opts: SingleSpaVueOpts) => {
176+
const bootstrap = async (opts: SingleSpaVueOpts<ExtraProps & Props>) => {
176177
if (opts.loadRootComponent) {
177178
const root = await opts.loadRootComponent();
178179
return (opts.rootComponent = root);
@@ -182,7 +183,7 @@ export default function singleSpaVue<ExtraProps>(
182183
};
183184

184185
const update = async (
185-
opts: SingleSpaVueOpts,
186+
opts: SingleSpaVueOpts<ExtraProps & Props>,
186187
mountedInstances: Record<string, Instance>,
187188
props: ExtraProps & AppProps,
188189
) => {
@@ -208,7 +209,7 @@ export default function singleSpaVue<ExtraProps>(
208209
};
209210

210211
const unmount = async (
211-
opts: SingleSpaVueOpts,
212+
opts: SingleSpaVueOpts<ExtraProps & Props>,
212213
mountedInstances: Record<string, Instance>,
213214
props: ExtraProps & AppProps,
214215
) => {

src/types/index.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
CreateAppFunction,
77
Vue2,
88
} from "vue-demi";
9-
import { AppProps, LifeCycleFn, ParcelProps } from "single-spa";
9+
import { AppProps, CustomProps, LifeCycleFn, ParcelProps } from "single-spa";
1010

1111
export type Vue = typeof Vue2;
1212

@@ -29,21 +29,25 @@ export interface BaseSingleSpaVueOptions {
2929
rootComponent?: Component;
3030
}
3131

32-
export type SingleSpaOptsVue2 = BaseSingleSpaVueOptions & {
33-
vueVersion: 2;
34-
appOptions: AppOptions;
35-
Vue: Vue;
36-
handleInstance?(app: Vue, props: Props): Promise<void> | void;
37-
};
38-
39-
export type SingleSpaOptsVue3 = BaseSingleSpaVueOptions & {
40-
vueVersion: 3;
41-
appOptions: AppOptions;
42-
createApp: CreateAppFunction<Element>;
43-
handleInstance?(app: App, props: Props): Promise<void> | void;
44-
};
45-
46-
export type SingleSpaVueOpts = SingleSpaOptsVue2 | SingleSpaOptsVue3;
32+
export type SingleSpaOptsVue2<ExtraProps = CustomProps> =
33+
BaseSingleSpaVueOptions & {
34+
vueVersion: 2;
35+
appOptions: AppOptions;
36+
Vue: Vue;
37+
handleInstance?(app: Vue, props: Props & ExtraProps): Promise<void> | void;
38+
};
39+
40+
export type SingleSpaOptsVue3<ExtraProps = CustomProps> =
41+
BaseSingleSpaVueOptions & {
42+
vueVersion: 3;
43+
appOptions: AppOptions;
44+
createApp: CreateAppFunction<Element>;
45+
handleInstance?(app: App, props: Props & ExtraProps): Promise<void> | void;
46+
};
47+
48+
export type SingleSpaVueOpts<ExtraProps = CustomProps> =
49+
| SingleSpaOptsVue2<ExtraProps>
50+
| SingleSpaOptsVue3<ExtraProps>;
4751

4852
export interface BaseInstance {
4953
domEl?: HTMLElement;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"noUnusedParameters": true,
1515
"noImplicitReturns": true,
1616
"skipLibCheck": true,
17-
"declarationDir": "dist",
17+
"outDir": "dist",
1818
"types": ["vite/client", "vitest/globals"],
1919
"paths": {
2020
"@/*": ["./src/*"]

0 commit comments

Comments
 (0)