Skip to content

Inferring ReturnType<T> from function with arguments falling back to any #29499

Closed

Description

TypeScript Version: 3.3.0-dev.20190119

Search Terms:
Infering type from function return type
Infering from return type
Argument breaks return type inferring

Code

export const createStyles = <
  K extends ReturnType<T>,  
  T extends (theme: { type: string }) => StyleSheet.NamedStyles<K> | StyleSheet.NamedStyles<any>,
>(
  fn: T
) => fn;

const styles = createStyles(() => ({
  logo: {
    width: 70,
    height: 70,
  },
}));

// here logo type is properly inferred from object
styles().logo.width


const styles = createStyles((theme) => ({
  logo: {
    width: 70,
    height: 70,
  },
}));

// here styles function signature is (theme: { type: string })  =>  any
styles({ type: 'dark' }).logo.width

Expected behavior:
It is possible to infer the return type of the function for both situations

Actual behavior:
Type inferring only for the function that doesn't have any parameters and falling to any if any parameters applied

Playground Link:
https://codesandbox.io/s/p3414yomnx

Related Issues:

Activity

  1. changed the title [-]Inferring from function with arguments falling back to any[/-] [+]Inferring ReturnType<T> from function with arguments falling back to any[/+] on Jan 20, 2019
  2. RyanCavanaugh commented on Jan 23, 2019

    @RyanCavanaugh
    Member

    Seems like you just have too many type parameters. This code works better:

    export const createStyles = <
      T extends (
        theme: { type: string }
      ) => StyleSheet.NamedStyles<unknown>
    >(
      fn: T
    ) => fn;
  3. dmtrKovalenko commented on Jan 26, 2019

    @dmtrKovalenko
    Author

    Ryan Cavanaugh (@RyanCavanaugh) Thanks for clarifying. But for react native it's required to infer type through NamedStyles to be sure that strings like textAlign are proper enum values.

    And here is a question - is it possible to make it works with inferring ReturnType through NamedStyles

  4. dmtrKovalenko commented on Jan 26, 2019

    @dmtrKovalenko
    Author

    I understand that here we getting circular dependency between types, referring each other. But maybe there is some way to not fall back to any?

  5. dmtrKovalenko commented on Jan 26, 2019

    @dmtrKovalenko
    Author

    And why it's breaking only if function has argument 🤔

  6. dmtrKovalenko commented on Feb 7, 2019

    @dmtrKovalenko
    Author

    Duplicate of #241

  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fix AvailableA PR has been opened for this issueQuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions