Skip to content

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

Closed
@dmtrKovalenko

Description

@dmtrKovalenko

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:

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