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

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

Closed
dmtrKovalenko opened this issue Jan 20, 2019 · 5 comments
Closed
Labels
Fix Available A PR has been opened for this issue Question An issue which isn't directly actionable in code

Comments

@dmtrKovalenko
Copy link

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:

@dmtrKovalenko dmtrKovalenko changed the title Inferring from function with arguments falling back to any Inferring ReturnType<T> from function with arguments falling back to any Jan 20, 2019
@RyanCavanaugh
Copy link
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;

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 23, 2019
@dmtrKovalenko
Copy link
Author

@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

@dmtrKovalenko
Copy link
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?

@dmtrKovalenko
Copy link
Author

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

@dmtrKovalenko
Copy link
Author

Duplicate of #241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Question An issue which isn't directly actionable in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants