Skip to content

ServiceIdentifier is invariant due to addition of Function #1036

@kenneth-devedis

Description

@kenneth-devedis

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

  1. Paste the above code snipping into any project using inversify
  2. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions