type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Unable to use prototype property of type constructed with Class utility

Open sempasha opened this issue 3 years ago • 0 comments

Problem

Unable to use prototype property of type constructed with Class utility.

Example

import { Class } from 'type-fest';
class Test {
  public property = 'value';
}
type TestProperty = Class<Test>['prototype']['property'];

(see script at TypeScript playground)

Expected

Expected TestProperty type to be string.

Actual

TestProperty type is any.

Affected TypeScript versions

Issue is actual for any TypeScript version available at TypeScript playbook at this moment (3.3.3 - 4.5.4).

Details

Seems that problem in the way TypeScript handles prototype property, which Class utility should add to result type. Class<Test>['prototype'] is any, while should be Test.

When prototype is renamed to proto, Class utility will work as expected, Class<Test>['proto'] will be Test, Class<Test>['proto']['property'] will be string.

type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {proto: T};
type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
class Test {
  public property = 'value';
}
type TestProperty = Class<Test>['proto']['property'];

(see script at TypeScript playground)

sempasha avatar Jan 14 '22 12:01 sempasha