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

Typed key accessor fails to infer type when used with generic #58153

Closed
Howard-Lam-UnitedVanning opened this issue Apr 11, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@Howard-Lam-UnitedVanning

πŸ”Ž Search Terms

Typed key access generic

πŸ•— Version & Regression Information

This is the behavior in every version I tried (4.9.5 and latest 5.4.4), and I reviewed the FAQ for entries about "Generics", "Type Guards"

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.4.4#code/JYOwLgpgTgZghgYwgAgJIBFgGcAOAbOATzgCM8UBvAKGVuTAHtNcDCAKASgC5kswpQAcwDcVAL5UwhHCgBiAVxAIAShDDyoILAGV+Q5AF5knQwD5eekCMnSUGbPiKlyAaQiEAPABVzR6rQBtF2RQZABrdwYYZC8AXR4vINjkCAAPSBAAEyw0ZkdiMhQAfmRgnhAIADdoUTEAiMIomNjRKRlkADU4PGBMt08fQ2R-ZCCQkHDI6LiEpJT0iCyc+xYnQuQAH2QFJVV1TR1LQWQSsuQK6qha+qnm0SpQSFhEFABRVLgAW3xKGnOviA8PgCKyiWhwQSA4Z-WiMPKsThAo5g5BiFEkL48TgGUzAoS1KgIBhaMDIIl4LA8Fb5ZwQfoed5fH6mALJIwBADkEIgHIANMgORjPnyBSAARyWoTiXwyQw8Dwuj0+u4GR9vuQWWzRlzISLBV89WLPjzJVQYIoEGBgMT6BA+ABGbymNipBL8hBU+FrVwqnwcaF0WUk20yoypAIISWBqBqDQTSB8AB0cIcCI4BPNSitNoTYAATE6XW6yQrur16X6A3QicHc0Nw5GUbRgNE2G0IE06wZuwLM5brSAOf6RtHY5oQ2BOE3USkKb9A7QY-t43awMmmKmiFOYajxGaLdmV3wAMxF5CM9UQd2lpX0i-M4c7msyrvIBtRugt4ztzurww9jk+0PIcq1HZcJ23QMxFnLB5wXJc4wndcvXYdMdwkMQgA

πŸ’» Code

interface IDisplayable {
    toDisplay(): string;
}
type FuncReturnsString = () => string;
type IDisplayableKey<T> = {
  [K in keyof T]: T[K] extends IDisplayable ? K : never;
}[keyof T];
type ValidKey<T> = {
  [K in keyof T]: T[K] extends IDisplayable | FuncReturnsString ? K : never;
}[keyof T];

interface Example {
  name: string;
  age: {
    toDisplay(): string;
  };
  bam: ()=>string;
}
const cols: IDisplayableKey<Example>[] = ['age', 'bam', 'name'];
const col: ValidKey<Example>[] = ['age', 'bam', 'name'];

function test1<T>(x: T, c: IDisplayableKey<T>) {
    const test = x[c];
    return test.toDisplay();
}
function test2<T>(x: T, c: ValidKey<T>) {
    const test = x[c];
    if (typeof test === 'function') {
      return test();
    } else {
      return test.toDisplay();
    }
}
function test3(x: Example, c: ValidKey<Example>) {
    const test = x[c];
    if (typeof test === 'function') {
      return test();
    } else {
      return test.toDisplay();
    }
}

πŸ™ Actual behavior

Compiler and intellisense fail to determine that test in test1, test2 functions have the toDisplay function even though they can determine that when T is explicitly stated as Example in test3. As a result, there are errors in the functions test1 and test2 but not test3.

πŸ™‚ Expected behavior

There should be no error with test and test2 because T[ValidKey[T]] can only return fields of type IDisplayable or function type ()=>string

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

You'd need #48992 for this

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 11, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants