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

When infer K type from value type, occured an error in generic T[K] #39972

Closed
HealGaren opened this issue Aug 10, 2020 · 1 comment
Closed

When infer K type from value type, occured an error in generic T[K] #39972

HealGaren opened this issue Aug 10, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@HealGaren
Copy link

TypeScript Version: 3.9.2

Search Terms: ts type inference by value type, ts get property name from value type

Expected behavior:
this is an example code of typescriptlang.org : Distributive conditional types
I got K type that T[K] is Function. (following code:)

I expected TypeScript to be able to properly infer value type using this K type.
testForT1 and TestForGeneric should works fine.

Actual behavior:
I can't get V into the proper type using the K I got.
testForT1 function worked, but testForGeneric function occured that error.

Type 'T[{ [K in keyof T]: T[K] extends Function ? K : never; }[keyof T]]' is not assignable to type 'Function'.
  Type 'T[T[keyof T] extends Function ? keyof T : never]' is not assignable to type 'Function'.
    Type 'T[keyof T]' is not assignable to type 'Function'.
      Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'Function'.
        Type 'T[string]' is not assignable to type 'Function'.(2322)

Related Issues:

Code

type FunctionPropertyNames<T> = {
  [K in keyof T]: T[K] extends Function ? K : never;
}[keyof T];
type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;

interface T1 {
    method(): string;
    method2(): number;
}

function testForT1(data: T1, key: FunctionPropertyNames<T1>): Function {
    return data[key];
}


function testForGeneric<T>(data: T, key: FunctionPropertyNames<T>): Function {
    return data[key];
}
Output
"use strict";
function testForI1(data, key) {
    return data[key];
}
function testForGeneric(data, key) {
    return data[key];
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@jack-williams
Copy link
Collaborator

Duplicate of #32550, and in particular see Ryan's comment.

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