question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it possible to infer superclass type parameters / constructor overload from the `super` call

See original GitHub issue

Search Terms

superclass base class generic type infer contextual infer parent generic super constructor

Suggestion

Make it possible to infer superclass type parameters / constructor overload from the super call

Use Cases

I’m working on an intermediate library with typings for a legacy codebase. That intermediate library is used in third party to use the functionality from the main codebase (called adapters). There are several supported ways to access this functionality:

  1. const foo1 = Adapter(options);
  2. const foo2 = new Adapter(options);
  3. class Foo3 extends Adapter { ... } - The constructor must call the superclass constructor with the options.

Now the problem is that the Adapter “class” (actually an ES5-style “function” class) has some properties that only exist if specific options are passed to its constructor. I’m able to model this behavior for case 1 and 2, but not 3 - at least not without manually specifying the type.

Here’s what I have so far. The code is split into three parts: main codebase, intermediate library, 3rd party / user code. (Playground link)

// legacy code, cannot change:
declare class InternalAdapter {
    // actually an ES5-style class function, can be called with and without new!
    someProp1: any;
    someProp2: any;

    // I want to override this type
    cacheObj: Record<string, any> | undefined;
}

// ========================

// My code, can change:
interface AdapterOptions {
    name: string;
    cache?: boolean;
}

type AdapterInstance<T extends AdapterOptions> = T extends {
	cache: true;
}
	? Omit<InternalAdapter, "cacheObj"> & {
			cacheObj: Exclude<InternalAdapter["cacheObj"], undefined>;
	  }
	: Omit<InternalAdapter, "cacheObj">;

interface AdapterConstructor {
	new (adapterName: string): AdapterInstance<{name: string}>;
	new <T extends AdapterOptions>(adapterOptions: T): AdapterInstance<T>;
    
	(adapterName: string): AdapterInstance<{name: string}>;
	<T extends AdapterOptions>(adapterOptions: T): AdapterInstance<T>;
}

declare const Adapter: AdapterConstructor;

// ========================

// User code, should be as simple as possible
const name = "foobar";
const options = { name };

const test1 = Adapter(name);
test1.cacheObj; // does not exist, expected

const test2 = new Adapter(name);
test2.cacheObj; // does not exist, expected

const test3 = Adapter(options);
test3.cacheObj; // does not exist, expected

const test4 = new Adapter(options);
test4.cacheObj; // does not exist, expected

const test5 = new Adapter({ ...options, cache: true });
test5.cacheObj; // exists, expected

// Here, the problems start:

class Test6 extends Adapter<AdapterOptions> {
    constructor(options: AdapterOptions) {
        super(options);
        this.cacheObj; // does not exist, expected
    }
}

class Test7 extends Adapter<AdapterOptions> {
    constructor(options: AdapterOptions) {
        super({ ...options, cache: true });
        this.cacheObj; // does not exist, unexpected
    }
}

class Test8 extends Adapter<AdapterOptions & {cache: true}> {
    constructor(options: AdapterOptions) {
        super({ ...options, cache: true });
        this.cacheObj; // exists, but I have to duplicate the type
    }
}

Notice how the class definitions are all awkward. In Test6 I have to duplicate the generic type AdapterOptions or the super call will default to the string constructor. In Test7 this is actually wrong, because the generic type overrides the conditional behavior for the cacheObj. This can be fixed like in Test8, but that is really ugly. IMO, TypeScript should be able to infer the type arguments from a constructor call if that type argument matches the class’ type argument

Examples

Show how this would be used and what the behavior would be - Ideally it should be like this:

class Test9 extends Adapter {
    constructor(options: AdapterOptions) {
        super({ ...options, cache: true });
        this.cacheObj; // should exist
    }
}

The super call would be used to infer that the 2nd constructor overload is the correct one:

new <T extends AdapterOptions>(adapterOptions: T): AdapterInstance<T>;

and therefore the instance type would be AdapterInstance<AdapterOptions & {cache: true}>.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code (not sure actually. I’d guess not?)
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:23
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
strutcodecommented, Jul 19, 2020

This missing feature has been a thorn in my side for some time and seems like an essential language addition. As a counter point to @tadhgmister 's counter point, the code as written should be broken.

The extended type T is more specific than number, therefor usages of T should be expected to be more specific than the default assigned type. In the example new Box(0) would disallow setting value = 1, while the subclass is allowed to do this using the same initial value. I would argue the current behavior presents like a bug.

Aside from correctness, inferring generics from super calls is essential because to the best of my knowledge there is currently no way to obtain the type of that parameter otherwise. Because super is a keyword with special behavior, if the intended behavior is for the ancestor class to receive the exact inferred type there’s not an alternate path via property access, inferred optional types or any other variety of black magic typing.

This particular issue makes it considerably more difficult to migrate a project containing hundreds of thousands of lines to Typescript where it would otherwise be a relatively painless transition. Please consider approving the proposal and I would be happy to look into submitting a pull request.

To clarify here’s a proposed use case:

class Form<Fields extends Record<string, string>> {
  constructor(private fieldTypes: Fields) {}

  getFieldValue(field: keyof Fields) {/* ... */}
}

class RegistrationForm extends Form {
  constructor() {
    super({
      firstName: 'text',
      lastName: 'text',
      email: 'email',
      password1: 'password',
      password2: 'password',
      terms: 'checkbox',
    })
  }
}

const form = new RegistrationForm()
form.getFieldValue(/* suggestions from inferred type go here */)
7reactions
matthewjhcommented, Oct 29, 2020

+1 to this

TypeScript, applied to certain use cases, e.g. subclassing, feels very verbose and unpleasant to work with at the moment. This is one of a few things needed to combat such verbosity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infer Generic Types of Superclass in Constructor TS
How can I make a TypeScript generic class to be constrained only to types that are an array of objects? 0 · TypeScript:...
Read more >
Using the Keyword super (The Java™ Tutorials > Learning the ...
With super() , the superclass no-argument constructor is called. With super(parameter list) , the superclass constructor with a matching parameter list is ...
Read more >
Documentation - Classes - TypeScript
Constructor (MDN). Class constructors are very similar to functions. You can add parameters with type annotations, default values, and overloads:.
Read more >
Trait Parameters - Scala 3
If a class C extends a parameterized trait T , and its superclass does as well, C must not pass arguments to T...
Read more >
Kinds of types - mypy 0.991 documentation
Any instance of a subclass is also compatible with all superclasses – it ... Mypy doesn't know anything about the possible runtime types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found