Skip to content

Commit

Permalink
fix typing (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx authored Jul 5, 2024
1 parent 9ccd017 commit ddf7504
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 501 deletions.
47 changes: 30 additions & 17 deletions ember-routable-component/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import Route from '@ember/routing/route';
import Component from '@glimmer/component';
import type { TemplateOnlyComponent } from '@ember/component/template-only';
import Controller from '@ember/controller';
import type Controller from '@ember/controller';

export interface Signature<T, Controller extends typeof Controller<T>> {
export interface Signature<
T = unknown,
C extends Controller<T> | unknown = Controller<T>,
> {
Args: {
controller: Controller;
controller: C;
model: T;
};
Blocks: {
Expand All @@ -14,17 +17,26 @@ export interface Signature<T, Controller extends typeof Controller<T>> {
};
}

export default function RoutableComponentRoute<
Model = unknown,
Controller extends typeof Controller<Model> = typeof Controller<Model>,
>(
Component: TemplateOnlyComponent<Signature<Model, Controller>>,
): typeof Route<Model>;
type GetModel<T> = T extends Controller<infer Model> ? Model : T;
type GetController<T> = T extends Controller ? T : Controller<T>;

type GetSignature<T> = Signature<GetModel<T>, GetController<T>>;

export default function RoutableComponentRoute<T>(
Component: TemplateOnlyComponent<GetSignature<T>>,
): typeof Route<GetModel<T>>;

export default function RoutableComponentRoute<T>(
component: typeof Component<GetSignature<T>>,
): typeof Route<GetModel<T>>;

export default function RoutableComponentRoute<
Model = unknown,
Controller extends typeof Controller<Model> = typeof Controller<Model>,
>(Component: Component<Signature<Model, Controller>>): typeof Route<Model>;
export default function RoutableComponentRoute(
component: Component,
): typeof Route<any>;

export default function RoutableComponentRoute(
component: TemplateOnlyComponent,
): typeof Route<any>;

export default function RoutableComponentRoute(Component: any) {
return class Rout extends Route {
Expand All @@ -43,7 +55,8 @@ export default function RoutableComponentRoute(Component: any) {
};
}

export class RoutableComponent<
T = unknown,
Controller extends typeof Controller<T> = typeof Controller<T>,
> extends Component<Signature<T, Controller>> {}
export class RoutableComponent<T = unknown> extends Component<
GetSignature<T>
> {}

export type RTOC<T> = TemplateOnlyComponent<GetSignature<T>>;
5 changes: 4 additions & 1 deletion ember-routable-component/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"compilerOptions": {
"allowJs": true,
"allowImportingTsExtensions": true,
"declarationDir": "declarations"
"declarationDir": "declarations",
"declaration": true,
"emitDeclarationOnly": true,
"noEmit": false
}
}
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Patrick Pircher",
"scripts": {
"build": "pnpm --filter ember-routable-component run build",
"lint": "pnpm --filter * run lint",
"lint": "pnpm --filter ember-routable-component --filter test-app run lint",
"lint:fix": "pnpm --filter * run lint:fix",
"prepare": "pnpm build",
"start": "concurrently 'npm:start:*' --restart-after 5000 --prefix-colors cyan,white,yellow",
Expand All @@ -19,13 +19,25 @@
},
"devDependencies": {
"@glint/core": "^1.4.0",
"@glint/environment-ember-loose": "^1.3.0",
"@glint/environment-ember-template-imports": "^1.3.0",
"@glint/environment-ember-loose": "^1.4.0",
"@glint/environment-ember-template-imports": "^1.4.0",
"@glint/template": "^1.4.0",
"concurrently": "^8.2.2",
"prettier": "^3.3.2",
"prettier-plugin-ember-template-tag": "^2.0.1",
"release-plan": "^0.9.0",
"typescript": "^5.5.3"
},
"workspaces": [
"ember-routable-component",
"test-app"
],
"pnpm": {
"overrides": {
"@glint/core": "^1.4.1-unstable.e8d1c9a",
"@glint/environment-ember-loose": "^1.4.1-unstable.e8d1c9a",
"@glint/environment-ember-template-imports": "^1.4.1-unstable.e8d1c9a",
"ember-eslint-parser": "^0.4.0"
}
}
}
Loading

0 comments on commit ddf7504

Please sign in to comment.