microsoft / microsoft/TypeScript

keyof of 'mixed' class equals to `string | number`

Open
#31,143 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Index Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.4.3 or 3.5.0-dev.20190427

Search Terms: keyof, anonymous class

Code

type AnyClass = new (...args: any[]) => any;

const mixin = <T extends AnyClass>(Class: T) => class extends Class {a: string};
const Cl = mixin(class {b = 123});
const inst = new Cl();

inst. //  <- IntelliSense works fine here and suggests 'a' and 'b' properties

type Keys = keyof typeof inst; // Keys = string | number

Expected behavior:
Keys to be equal to 'a' | 'b'

Actual behavior:
Keys to be equal to string | number

Playground Link: ts-playground

Description:
I'm sorry if that is a known bug ot expected behaviour. I did not find a duplicate.
Class C is a result of extension of some other class A provided as a function argument and class B which is declared inside the function body. Such class C works fine for the most part, but keyof InstanceType<typeof C> returns string | number.

image

While IntelliSense works fine:
image

I'll be glad if you suggest any workarounds for this issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example in the linked TypeScript Playground using the reported versions and confirm the inferred type of keyof typeof inst. Trace the compiler's handling of keyof for the mixed anonymous class and add coverage for the example so that the result is 'a' | 'b' rather than string | number.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.