Skip to content

Cannot select object type from wrapping object using parameter #52399

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

Closed
wbt opened this issue Jan 24, 2023 · 1 comment · Fixed by #53059
Closed

Cannot select object type from wrapping object using parameter #52399

wbt opened this issue Jan 24, 2023 · 1 comment · Fixed by #53059
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@wbt
Copy link

wbt commented Jan 24, 2023

Bug Report

🔎 Search Terms

Deferred parameter select generic index

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type parameters
  • Nightly version at time of test: v5.0.0-dev.20230123

⏯ Playground Link

Playground link with relevant code

💻 Code

type DataFetchFns = {
    Boat: {
        requiresLicense: (id: string) => boolean;
        maxGroundSpeed: (id: string) => number;
        description: (id: string) => string;
        displacement: (id: string) => number;
        name: (id: string) => string;
    };
    Plane: {
        requiresLicense: (id: string) => boolean;
        maxGroundSpeed: (id: string) => number;
        maxTakeoffWeight: (id: string) => number;
        maxCruisingAltitude: (id: string) => number;
        name: (id: string) => string;
    }
}
//Can select non-object type from wrapping object using parameter:
export type NoTypeParamBoatRequired<F extends keyof DataFetchFns['Boat']> = ReturnType<DataFetchFns['Boat'][F]>;
type TypeHardcodedAsParameterWithoutReturnType<T extends 'Boat', F extends keyof DataFetchFns[T]> = DataFetchFns[T][F];
export type allAreFunctionsAsExpected = TypeHardcodedAsParameterWithoutReturnType<'Boat', keyof DataFetchFns['Boat']>;
// Above line is as expected: ((id: string) => boolean) | ((id: string) => number) |
// ((id: string) => string) | ((id: string) => number) | ((id: string) => string)
export type returnTypeOfFunctions = ReturnType<allAreFunctionsAsExpected>; //string | number | boolean as expected
export type SucceedingCombo = ReturnType<TypeHardcodedAsParameterWithoutReturnType<'Boat', keyof DataFetchFns['Boat']>>;
//Now try putting those definitions of TypeHardcodedAsParameterWithoutReturnType & returnTypeOfFunctions together: it fails.
//Why should the below fail while the line above succeeds? Error given is
//Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
export type FailingCombo<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<TypeHardcodedAsParameterWithoutReturnType<T,F>>;
export type TypeHardcodedAsParameter<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
type VehicleSelector<T extends keyof DataFetchFns> = DataFetchFns[T]; //workaround attempt that doesn't work
export type TypeHardcodedAsParameter2<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<VehicleSelector<T>[F]>;
//Eventual goal:
export type TypeGeneric<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;

🙁 Actual behavior

Errors as shown, TypeScript can't figure out that the contents of ReturnType<> are function types, when a generic type parameter from the left side of a type definition is used in the right side, though it can in the definition of allAreFunctionsAsExpected.

🙂 Expected behavior

No errors in this example. All of the type definitions throwing errors should simplify to string | number | boolean in this toy example.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Help Wanted You can do this Experience Enhancement Noncontroversial enhancements labels Jan 24, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 24, 2023
@islandryu
Copy link
Contributor

I'll work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants