Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ElementType does not work with keyofStringsOnly TS option #179

Open
Knagis opened this issue Oct 31, 2022 · 2 comments · May be fixed by #180
Open

$ElementType does not work with keyofStringsOnly TS option #179

Knagis opened this issue Oct 31, 2022 · 2 comments · May be fixed by #180

Comments

@Knagis
Copy link

Knagis commented Oct 31, 2022

Description

export declare type $ElementType<T extends {
    [P in K & any]: any;
}, K extends keyof T | number> = T[K];

with keyofStringsOnly: true

Type 'K' cannot be used to index type 'T'.(2536)

Steps to Reproduce

open https://www.typescriptlang.org/play?keyofStringsOnly=true&ssl=3&ssc=39&pln=1&pc=1#code/KYDwDg9gTgLgBAE2AYwDYEMrDjAnmbAEgFFVgBbYAOxgBV9gAeWuUGahAZzgG8AoOILgBtAApwAllTgBpOADI46KrgC6ALiUqA3HwC+AGlmsQ7KlzgBrYLggAzOCwA+cKgFdyAI2BQAfHABeR2EZVV0BQQB6SLhicBQYTU4YKCkAc10hPAI4USgIMG4gnld0SiSU9O0lNOBNdy8faoA3CU4JTzJNTwgIMmU4PUzBbOwAOTLgehygkjJKGmmmPILOIwByKkn133ChaNj45ES4Hr7gZQicBkc3MDJAkTP+qiMG7ygwq9G4AEFHuYUah0BjMO5kDYABh2wzgBziBGO9Q8H2+NwAQgDSEDFqDaODgBsAIwwvhXeFHE7PC5UNE5X5QKCPanKYRfLI3ACS7HInCWWPmwKWjAZUDeKJ8uzJ+xiCISyMaUDp2AA8p4AFaPErCay4CqpKhpDSuCVMobKuAANXQqDcwD5N1m2IWIIIjDV6qMyQNaSlQA

Expected behavior

no TS compilation errors

Suggested solution(s)

export declare type $ElementType<T extends {
    [P in K & any]: any;
}, K extends keyof T | number> = T extends [] ? T[K] : T[Exclude<K, number>];

this passes the examples given in the file for both when keyofStringsOnly is enabled and disabled.

Project Dependencies

  • Utility-Types Version: 3.10.0
  • TypeScript Version: 4.7.4, 4.8.4
  • tsconfig.json:
@Knagis
Copy link
Author

Knagis commented Oct 31, 2022

slightly better approach, does not exclude number when keyof T does include it:

export declare type $ElementType<T extends {
    [P in K & any]: any;
}, K extends keyof T | number> = T extends [] ? T[K] : T[Exclude<K, Exclude<number, keyof any>>];

@piotrwitek
Copy link
Owner

Hey @Knagis , thanks for the report and solution, could you please submit a PR?

Knagis added a commit to Knagis/utility-types that referenced this issue Nov 4, 2022
@Knagis Knagis linked a pull request Nov 4, 2022 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants