From 735f5430717c51e1c5d92e745f964d25651dda47 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:47:24 -0400 Subject: [PATCH] wip --- ember-resources/package.json | 1 + ember-resources/src/function-based/resource.ts | 10 +++++----- ember-resources/src/function-based/types.ts | 6 +++++- ember-resources/src/index.ts | 2 +- test-app/package.json | 1 + test-app/tests/type-tests/function-based.ts | 2 +- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ember-resources/package.json b/ember-resources/package.json index d9cd9c993..99c8b3ed5 100644 --- a/ember-resources/package.json +++ b/ember-resources/package.json @@ -38,6 +38,7 @@ "start": "pnpm vite build --watch", "build": "pnpm vite build", "lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'", + "lint:types": "tsc --noEmit", "lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'", "lint:js": "eslint . --cache", "lint:prettier": "prettier --check '**/*.{js,ts}'", diff --git a/ember-resources/src/function-based/resource.ts b/ember-resources/src/function-based/resource.ts index a1c94318c..6085d361e 100644 --- a/ember-resources/src/function-based/resource.ts +++ b/ember-resources/src/function-based/resource.ts @@ -7,7 +7,7 @@ import { ResourceManagerFactory } from './manager.ts'; import { INTERNAL } from './types.ts'; import { wrapForPlainUsage } from './utils.ts'; -import type { InternalFunctionResourceConfig, ResourceFn, ResourceFunction } from './types.ts'; +import type { InternalFunctionResourceConfig, Resource, ResourceFunction } from './types.ts'; const TYPE = 'function-based'; @@ -111,7 +111,7 @@ registerUsable(TYPE, (context: object, config: InternalFunctionResourceConfig) = * * ``` */ -export function resource(setup: ResourceFunction): Value; +export function resource(setup: ResourceFunction): Resource; /** * `resource` is an alternative API to the class-based `Resource`. @@ -167,14 +167,14 @@ export function resource(setup: ResourceFunction): Value; * } * ``` */ -export function resource(context: object, setup: ResourceFunction): Value; +export function resource(context: object, setup: ResourceFunction): Resource; /** */ export function resource( context: object | ResourceFunction, setup?: ResourceFunction, -): Value | InternalFunctionResourceConfig | ResourceFn { +): Value | InternalFunctionResourceConfig | Resource { if (!setup) { assert( `When using \`resource\` with @use, ` + @@ -205,7 +205,7 @@ export function resource( * using vanilla functions as resources without the resource wrapper * */ - return internalConfig as unknown as ResourceFn; + return internalConfig as unknown as Resource; } assert( diff --git a/ember-resources/src/function-based/types.ts b/ember-resources/src/function-based/types.ts index 289db3fef..297348a5b 100644 --- a/ember-resources/src/function-based/types.ts +++ b/ember-resources/src/function-based/types.ts @@ -131,7 +131,11 @@ export type ResourceFunction = (hooks: ResourceAPI) => Value | * of the resource is the result of the collapsed functions * passed to `resource` */ -export type ResourceFn = Value & ((hooks: ResourceAPI) => Value); +export interface Invokable extends Reactive { + (hooks: ResourceAPI): Value; +} + +export type Resource = Value & Invokable; export type Destructor = () => void; export type Cache = object; diff --git a/ember-resources/src/index.ts b/ember-resources/src/index.ts index 4e71efdb4..42fce279f 100644 --- a/ember-resources/src/index.ts +++ b/ember-resources/src/index.ts @@ -5,4 +5,4 @@ export { registerUsable, use } from './use.ts'; // Public Type Utilities export type { ResourceAPI } from './function-based/index.ts'; -export type { Reactive } from './function-based/types.ts'; +export type { Reactive, Resource } from './function-based/types.ts'; diff --git a/test-app/package.json b/test-app/package.json index 56968fab4..a2d26e8d5 100644 --- a/test-app/package.json +++ b/test-app/package.json @@ -12,6 +12,7 @@ }, "scripts": { "lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'", + "lint:types": "glint", "lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'", "lint:hbs": "ember-template-lint . --no-error-on-unmatched-pattern", "lint:js": "eslint . --cache", diff --git a/test-app/tests/type-tests/function-based.ts b/test-app/tests/type-tests/function-based.ts index d11cb3401..b263972f6 100644 --- a/test-app/tests/type-tests/function-based.ts +++ b/test-app/tests/type-tests/function-based.ts @@ -1,4 +1,4 @@ -import { resource } from 'ember-resources'; +import { type Resource, resource } from 'ember-resources'; import { expectTypeOf } from 'expect-type'; import { expectType } from 'ts-expect';