Is it possible to combine two types to create another TypeLiteral?

typescript type vs interface
typescript record
typescript keyof
typescript object type
typescript dictionary type
typescript index signature
typescript interface
typescript function type

Given this method signature, is it possible to implement it? If so, how?

TypeLiteral<MyClass<T, R>> combineTypes(Class<T> typeOne, Class<R> typeTwo) {
  // Awesome implementation here


I have an interface, ApiHandler<TReq, TRes> which I am trying to create in a factory, with Guice, given type TReq. I can also pass in the type TRes if necessary. Unfortunately, the TReq and TRes have no meaningful parent classes or interfaces for reasons beyond my control (they are generated from Apache Thrift).

If you're computing it based on runtime arguments, it's no longer a literal: It's just a Type.

Though you could use Guava's equivalent framework of TypeToken and its utilities, there's a Type utility class built into Guice: Use newParameterizedType(Type rawType, Type... typeArguments) to create the Type you want, noting that ParameterizedType and Class both implement Type.

static ParameterizedType combineTypes(Class<?> typeOne, Class<?> typeTwo) {
  return Types.newParameterizedType(MyClass.class, typeOne, typeTwo);

Unfortunately, AbstractModule.bind and don't offer overloads for Type; just Class, TypeLiteral, and Key. Luckily, you can generate a Key reflectively using a Type, using Key.get(Type):

bind(Key.get(combineTypes(Foo.class, Bar.class))).to(MyClassFooBar.class);

Note, in this, that ParameterizedType is not itself a parameterized type. This defeats some of the clever generics-based protection that Guice's bind EDSL offers. To get the above to work, you may need to @SuppressWarnings, return the raw type Key, or consider having combineTypes return a Key<MyClass<T, R>> (which would require a cast from Key.get(Type)'s return value Key<?>). If you really must use a TypeLiteral, you can produce a TypeLiteral through Key.getTypeLiteral, but that would also require a cast from TypeLiteral<?>—and would not be a "type literal" by any meaningful definition.

Advanced Types · TypeScript, A common idiom in JavaScript to differentiate between two possible values is to These typeof type guards are recognized in two different forms: typeof v Aliasing doesn't actually create a new type - it creates a new name to refer to that type. In practice string literal types combine nicely with union types, type guards,  If you take two different species of animal these are not able to combine and make a new one naturally. However, some plants are able to do this. When two plant species combine to make a new one it is called hybridization and there are quite a lot of examples of this that have occurred naturally in the wild.

TypeLiteral has two constructors; the normal one when used as intended TypeLiteral<MyClass<Foo, Bar>>() {}, and the unsafe one which is not generic and is created directly: new TypeLiteral(type). Looking at the code for Guice's TypeLiteral here, we see the regular constructor uses parameterized.getActualTypeArguments()[0] for this parameter, where parameterized is a ParameterizedType. To make something that matches MyClass<R, T>, this would be another ParameterizedType, but with two parameters, not one, and each parameter would be a plain class. You can now create an implementation of the ParameterizedType interface that fulfills the contract needed, construct a TypeLiteral, and cast it to the correct return value (it won't have the correct generic type, you'll have to do an unsafe cast).

It looks like this:

TypeLiteral<MyClass<T, R>> combineTypes(Class<T> typeOne, Class<R> typeTwo) {
    return new TypeLiteral(new ParameterizedType() {
        Type[] getActualTypeArguments() {
            return new Type[] {typeOne, typeTwo};

        Type getRawType() {
            return MyClass.class;

        Type getOwnerType() {
            // this is only needed for nested classes, eg. if this was Foo.Myclass this would return Foo.class.
            return null;


Knowledge-Based Intelligent Information and Engineering Systems: , On the other hand, some more sophisticated maintenance events can be managed using For instance suppose we designed an ontology where two sibling classes exist: is not useful in our domain and we decide to merge these two classes. The newly created class, for example Professor, will have as properties and  In such case you have to create the combination of the two chart types manually: First set up a normal stacked column chart with all the data in it (also the data for the line chart). Next, click on the column in the chart which contains the values for the line chart. Right click on it “Change Chart Type” and select the desired chart type.

I didn't use Guice or Thrift before, hope this can help.

Definition of MyClass: public class MyClass<T, R> {}

Implement the method by adding type parameter:

public static <T, R> TypeLiteral<MyClass<T, R>> combineTypes(Class<T> typeOne, Class<R> typeTwo) {
    return new TypeLiteral<MyClass<T, R>>() {};
public static void main(String[] args) {
    TypeLiteral<MyClass<Integer, String>> myClassTypeLiteral = TypeTest.combineTypes(Integer.class, String.class);

Or simply:

public static <T, R> TypeLiteral<MyClass<T, R>> combineTypes() {
    return new TypeLiteral<MyClass<T, R>>() {};
public static void main(String[] args) {
    TypeLiteral<MyClass<Integer, String>> myClassTypeLiteral = TypeTest.<Integer, String>combineTypes();

TypeLiteral (Guice latest API), For example, to create a type literal for List<String> , you can create an empty anonymous inner class: Along with modeling generic types, this class can resolve type parameters. For example, to figure out what Constructs a new type literal. For example you may want to plot actual revenue with forecasted revenue to compare the two values. In an earlier post , we showed you how to create combination charts in Excel 2010, but we have revamped this experience as a part of Excel 2013 to make it much easier to create combo charts.

Interface vs Type alias in TypeScript 2.7 - Martin Hochel, Type aliases don't create a new name — for instance, error messages won't use the alias name Let's define compile time types for Point via interface and type alias and 2 Point defined with interface and type literal We can also combine both type alias and interface for implementing a Class constraint. It's not possible to merge the results of two separate surveys, even if they are identical in design or copies of one another. If you need to merge the data of two separate surveys, export the data for each survey and try combining the files in Excel.

Type-Safe Object Merging in TypeScript 2.8, There are times when you want to merge two generic types in The task I want to accomplish is to create a generic function capable of merging aObj isn't in U. Exclude is a new type in TypeScript 2.8 in which everything  It is possible to combine the mortgages from two properties into one mortgage. To achieve this, you would need to refinance by taking out a larger loan on one home, and using the money to pay off

PEP 586 -- Literal Types, This PEP proposes adding Literal types to the PEP 484 ecosystem. Python has many APIs that return different types depending on the value of So, if we have some variable foo of type Literal[3] it's safe to do things like foo + 5 For example​, we can combine Literal[True], Literal[False], and overloads to  Big storage How to combine multiple hard drives into one volume on Windows 10 You can group two or more hard drives on your Windows 10 PC to create a single large volume to store your data.

  • Do you have access to Guava's TypeToken? You basically need to build a new ParameterizedType object. Alternatively, you need to change your API so that your factory's caller constructs the TypeLiteral and passes it in.
  • Yes, I have access to Guava. Could you elaborate more on your suggestion with ParameterizedType object? Also, re: your second suggestion, you mean the caller passes in a TypeLiteral<MyClass<T, R>>, right?
  • Thanks for helping me understand (well, at least more than yesterday) these topics!
  • Will try it out today and report back!
  • Bad news, the unsafe constructor is private to the package and I cannot invoke it. Though, your second suggestion up above seems more promising? Unless you have any other ideas to get it working it the way of the first suggestion, do you mind editing your answer to the second suggestion and I'll mark it as accepted?
  • It actually looks like Jeff Bowman's answer provides a better solution; you should use that instead.
  • I don't think that will work. The TypeLiteral won't capture runtime types: it will just close over the type variables, which won't help here.