At the moment, there is no specific syntax available to "skip" a generic type argument for default values or inference by the compiler. There has been a long-standing feature request open for this at microsoft/TypeScript#26242. If this feature ever gets implemented, you might be able to use something like Foo<number, , number>
, Foo<number, *, number>
, or Foo<number, infer, number>
.
Until that happens, the workaround is to explicitly define the types as in Foo<number, string, number>
, or find alternative solutions.
If your intention of "skipping" is to obtain the default value, one workaround is to create a custom type to represent "skip," then adjust your type accordingly. For example:
interface $ { ___SIGIL___: true; } // $ denotes "skip"
type Default<T, U> = [T] extends [$] ? U : T;
Instead of type F<T = X, U = Y, V = Z>
, you would now write
type MyF<T = $, U = $, V = $> = F<Default<T, X>, Default<U, Y>, Default<V, Z>>
. For a type like
Foo
, it could look like:
type FooT1Default = Foo<any> extends Foo<any, infer T1, any> ? T1 : never;
// type FooT1Default = string
type FooT2Default = Foo<any> extends Foo<any, any, infer T2> ? T2 : never;
// type FooT2Default = boolean
type MyFoo<T, T1=$, T2=$> =
Foo<T, Default<T1, FooT1Default>, Default<T2, FooT2Default>>;
You can test it with:
type Example = MyFoo
// type Example = Foo<number, string, number>
const data: MyFoo = {
ID: 1,
Name: "apache", // okay
IsActive: 1,
}
Visit the Playground link for this code snippet