1

In Rust is possible to omit a type parameter using _, which infers the concrete type. Is there any way of doing the same in TypeScript? So for example, I have this now, but I want to omit the first type parameter in open() (see second snippet).

(This open is supposed to be the same as MatDialog.open in Angular)

// -----
// TYPES
// -----
interface Params<T> {
  data: T;
}

function open<T, D = any>(component: ComponentType<T>, params: Params<D>) {
  // ...
}

// -----
// USAGE
// -----
interface MyComponentData {
  foo: number;
}

open<MyComponent, MyComponentData>(MyComponent, {
  data: {
    foo: 42,
  },
})

The real question, is it possible to omit T so it's inferred from the usage? For example, using something like _ as placeholder (which doesn't work):

open<_, MyComponentData>(MyComponent, {
  data: {
    foo: 42,
  },
})
doup
  • 841
  • 1
  • 9
  • 20
  • 2
    Not really. TypeScript does not have partial type inference. There is a [proposal](https://github.com/microsoft/TypeScript/issues/26242) for a syntax similar to what you have in your question. – Tobias S. Oct 26 '22 at 12:12

0 Answers0