Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflict test case found for type definition. #1566

Open
TotooriaHyperion opened this issue Apr 19, 2024 · 2 comments
Open

Conflict test case found for type definition. #1566

TotooriaHyperion opened this issue Apr 19, 2024 · 2 comments

Comments

@TotooriaHyperion
Copy link

TotooriaHyperion commented Apr 19, 2024

I'm trying to improve BindingToSyntax type definition because it lost type check for toConstructor, toFactory to be compatible with type inferred from bind(XXX) ...etc

But I found conflict test cases:
image

Expected Behavior

types between BindingToSyntax should be compatible with generic type param of toConstructor.

Current Behavior

The right bottom example of BindingToSyntax<T> requires toConstructor(constructor: Newable<T>)
But the rest 3 examples of BindingToSyntax<Newable<T>> requires toConstructor(constructor: Newable<T>)

Possible Solution

image

This gives the toConstructor ability to infer the type from BindingToSyntax<T> if T is Newable or returns type never

But the Ninja test fails because it's using wrong types.

I need to change the test case of toConstructor<Ninja>(Ninja).

Steps to Reproduce (for bugs)

Context

Your Environment

  • Version used:
  • Environment name and version (e.g. Chrome 39, node.js 5.4):
  • Operating System and version (desktop or mobile):
  • Link to your project:

Stack trace

@TotooriaHyperion TotooriaHyperion changed the title Ambiguous test case found for type definition. Conflict test case found for type definition. Apr 19, 2024
@notaphplover
Copy link
Member

notaphplover commented Nov 14, 2024

Hey @TotooriaHyperion, I see, your proposal makes a lot of sense, but typescript is a bit tricky. I think there's an edge case we should try to cover:

Consider the following code example:

import { interfaces, LazyServiceIdentifer } from 'inversify';

type ExtendsLazyServiceIdentifier =
  LazyServiceIdentifer extends interfaces.Newable<LazyServiceIdentifer>
    ? true
    : false;

What type do you think is ExtendsLazyServiceIdentifier? The answer is, surprisingly false.

tsc declared types are not inferred as extending new (...args: any[]) => infer T, which is shocking.

I think I'll go to the tsc repo and ask why T is a new (...args: any[]) => infer T but only extends new (...args: any[]) => infer T when the class is not declared by tsc. Until that we will need to find a better solution.

EDIT: It seems https://stackoverflow.com/a/53056911 explains this weird behavior, I was not aware of this static-instance type duality of classes.

@notaphplover
Copy link
Member

What about:

public toConstructor<T2 extends T>(
    constructor: interfaces.Newable<T2>,
  ): interfaces.BindingWhenOnSyntax<T>;

I think it would work nicely that way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants