Skip to content

Commit

Permalink
fix: correctly resolve infer type from abstract classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Aug 27, 2024
1 parent 40c8d85 commit 230e7bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/api-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ declare const assertType: <R, U = [R]>(result: [R] extends U ? R : never) => voi

function testResolve() {

function testAbstractClass() {
abstract class A {
a = 1;
}

const a = resolve(A);
if (a.a === 2) {

}
}

function testResolveSingle() {
class A { a = 1 }
class B { b = '2' }
Expand Down
34 changes: 18 additions & 16 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -608,23 +608,25 @@ export class Container {
}

export type ResolvedValue<T> =
T extends { new (...args: any[]): infer R }
T extends (new (...args: any[]) => infer R)
? R
: T extends Factory<infer R>
? (...args: unknown[]) => R
: T extends Lazy<infer R>
? () => R
: T extends NewInstance<infer R>
? R
: T extends Optional<infer R>
? R | null
: T extends All<infer R>
? R[]
: T extends Parent<infer R>
? R | null
: T extends [infer T1, ...infer T2]
? [ResolvedValue<T1>, ...ResolvedValue<T2>]
: T;
: T extends (abstract new (...args: any[]) => infer R)
? R
: T extends Factory<infer R>
? (...args: unknown[]) => R
: T extends Lazy<infer R>
? () => R
: T extends NewInstance<infer R>
? R
: T extends Optional<infer R>
? R | null
: T extends All<infer R>
? R[]
: T extends Parent<infer R>
? R | null
: T extends [infer T1, ...infer T2]
? [ResolvedValue<T1>, ...ResolvedValue<T2>]
: T;

/**
* Resolve a key, or list of keys based on the current container.
Expand Down

0 comments on commit 230e7bd

Please sign in to comment.