microsoft / microsoft/TypeScript
Cannot override method in subclass when superclass instance type is a mapped type
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.1.1
Search Terms:
defines instance member property
Code
class A {
foo(): void {};
bar: number;
}
class B extends (A as { new(): Pick<A, Exclude<keyof A, "foo">> & Pick<A, "foo"> }) {
// (1)
foo(): void {
super.foo();
}
baz: number;
}
Expected behavior:
Compiles successfully
Actual behavior:
Error at (1): [ts] Class 'Pick<A, "bar"> & Pick<A, "foo">' defines instance member property 'foo',
but extended class 'B' defines it as instance member function.
A more realistic example of this is when using https://github.com/bterlson/strict-event-emitter-types along with subclassing a superclass with overridable methods:
import StrictEventEmitter from "strict-event-emitter-types";
import * as inspector from "inspector";
interface SessionEvents {
"Runtime.executionContextCreated": (message: inspector.InspectorNotification<Runtime.ExecutionContextCreatedEventDataType>) => void
}
type StrictSession = StrictEventEmitter<inspector.Session, SessionEvents>;
class CustomSession extends (inspector.Session as { new (): StrictSession }) {
connect() { // error here
super.connect();
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the TypeScript code sample and Playground link to reproduce the diagnostic involving Pick, Exclude, and the subclass override. Trace how the compiler compares the mapped superclass member with B.foo, then add coverage for the example and confirm the valid override compiles without an error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100