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

Indexed access type with generics #201

Open
uittorio opened this issue Jan 29, 2020 · 0 comments
Open

Indexed access type with generics #201

uittorio opened this issue Jan 29, 2020 · 0 comments

Comments

@uittorio
Copy link
Member

uittorio commented Jan 29, 2020

I've found a couple of issues related to indexed access type

NOTE: in definitely typed test results this may be references as
Cannot read property 'declarations' of undefined

Class[T]

Example 1

interface WithKeyGeneric<R> {
  test<U extends keyof R>(): WithGeneric<R[U]>
}

class WithGeneric<T> {
  public a: T;
}

const type = createMock<WithKeyGeneric();

This will fail to get R[U] for 2 reasons;

  • the generic is not provided
  • the type parameter used to find a type R is using currently PropertySignatureCache that is meant to be used to store properties name. (I'm not sure if can always work). Example 2 give more explanation about this

Example 2

class Normal {
  public a: number;
  public b: string;
}

interface Test<T extends keyof Normal> {
  prop: Normal[T]
}

const type = createMock<Test<'a'>>();
const type2 = createMock<Test<'b'>>();

type should have a prop with number 0 /// it works
type2 should have a prop with string '' // it doesn't work

The reason is failing is because we are not storing the generic correctly from Test<'a'> or Test<'b'> and we are not getting the correct property T because of the same reason of Example 1

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

No branches or pull requests

1 participant