Skip to content

Instantly share code, notes, and snippets.

@peanutbother
Created October 29, 2019 12:39
Show Gist options
  • Save peanutbother/f68268308efc346caffa73552997533e to your computer and use it in GitHub Desktop.
Save peanutbother/f68268308efc346caffa73552997533e to your computer and use it in GitHub Desktop.
Wrap Typescript Types

Example How To Wrap All Properties In Interface Or Tuple

This example shows how to wrap all properties of an interface or tuple dynamically with a wrapper type. This is possible by declaring a dynamic index with a key variable which maps property keys with the original type and using this key to refer to the wrapped type property.

The second example is more dynamic by providing a type descriptor T which provides the wrapper type which then receives the keys and types of the wrapped type.

// your union type or tuple
type MyTuple = {
prop1 : string;
prop2 : number;
}
// your wrapped union type
type WrappedTuple = {
// [index] = WrapperType < YourType [index] >
[P in keyof myTuple] = Promise<MyTuple[P]>
}
// your dynamically wrapped union type
type WrappedTuple<T> = {
// [index] = DynamicWrapperType < YourType [index] >
[P in keyof myTuple] = T<MyTuple[P]>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment