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

Constructor type inference #2893

Open
mattjohnsonpint opened this issue Jan 11, 2025 · 3 comments · May be fixed by #2894
Open

Constructor type inference #2893

mattjohnsonpint opened this issue Jan 11, 2025 · 3 comments · May be fixed by #2894

Comments

@mattjohnsonpint
Copy link
Contributor

Feature suggestion

Consider a generic class, such as:

class Foo<T> {
  constructor(public value: T) {}
}

This works:

const foo = new Foo<string>("abc");

But this does not, currently:

const foo = new Foo("abc");

It fails with:

ERROR TS2558: Expected 1 type arguments, but got 0.
   :
 5 │ const foo = new Foo("abc");
   │             ~~~~~~~~~~~~~~
   └─ in assembly/index.ts(5,13)

I would expect type inference to work on constructors, the same as it presently does for functions. Consider that this does work:

function createFoo<T>(value: T): Foo<T> {
  return new Foo(value);
}

const foo = createFoo("abc");

And type inference on static methods works too:

class Foo<T> {
  constructor(public value: T) {}

  static create<T>(value: T): Foo<T> {
    return new Foo(value);
  }
}

const foo = Foo.create("abc");
@JairusSW
Copy link
Contributor

@CountBleck do you have time to take a look at this? It should be simple--I tried, but im not too familiar with the codebase

@CountBleck
Copy link
Member

Hm, if type inference exists for generic functions, it should be relatively simple to implement for constructors (famous last words).

@mattjohnsonpint
Copy link
Contributor Author

I may dig into this myself a bit. 🤞

@mattjohnsonpint mattjohnsonpint linked a pull request Jan 12, 2025 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants