-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
The ServiceIdentifier
Type was changed from: string | symbol | Newable<T>
to string | symbol | Newable<T> | Function
.
I think the issue here is that Function
is a super type of Newable<T>
and makes ServiceIdentifier
invariant to its generic type argument.
This means that the following typescript code is valid:
class SomeClass {}
const barString = 'bar' as ServiceIdentifier<number>
const barSymbol = Symbol('bar') as ServiceIdentifier<number>
const barNewable = Number
const wrongNewable: ServiceIdentifier<string> = SomeClass
let foo: ServiceIdentifier<string>
foo = barString
foo = barSymbol
foo = barNewable
foo = wrongNewable
let fooTuple: [ServiceIdentifier<string>]
fooTuple = [barString]
fooTuple = [barSymbol]
fooTuple = [barNewable]
fooTuple = [wrongNewable]
This also means that BindToFluentSyntax.toResolvedValue
is actually not typesafe.
Steps to reproduce
- Paste the above code snipping into any project using inversify
- type-check using
tsc
Expected behavior
the above code snippet causes typescript to fail.
Possible solution
Replace the Function
in the ServiceIdentifier
union type, with something more typesafe.
export type TypedFunction<TInstance = unknown, TArgs extends unknown[] = any[]> = (...args: TArgs) => TInstance;
type ServiceIdentifier<TInstance = unknown> = string | symbol | Newable<TInstance> | TypedFunction<T>;
Not sure if this is considered a breaking change.
Package version
7.5.1
Node.js version
22.16.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Stack trace
No response
Other
No response
Metadata
Metadata
Assignees
Labels
No labels