mobily / ts-belt

🔧 Fast, modern, and practical utility library for FP in TypeScript.
https://mobily.github.io/ts-belt
MIT License
1.11k stars 31 forks source link

TypeScript doesn't use the name of the type while inferring return type #43

Closed jbhoot closed 2 years ago

jbhoot commented 2 years ago

For a function

const toFirstName = (input: string) => R.fromPredicate(input, S.isNotEmpty, 'Enter your first name');

the return type is inferred as

Screenshot from 2022-09-06 11-28-03

which is technically true, but I would have preferred the inferred type to be the same as that of R.fromPredicate()

Screenshot from 2022-09-06 11-29-40

Right now, I need to specify the return type explicitly as

const toFirstName = (input: string): Result<string, string> => R.fromPredicate(input, S.isNotEmpty, 'Enter your first name');
mobily commented 2 years ago

@jayesh-bhoot I assume that something is wrong on your side, on my end the type is inferred and narrowed correctly:

CleanShot 2022-09-06 at 16 29 09@2x CleanShot 2022-09-06 at 16 29 28@2x

it works fine with flow as well:

CleanShot 2022-09-06 at 16 31 15@2x
jbhoot commented 2 years ago

You are right. This problem shows up on IntelliJ WebStorm, but not on Visual Studio Code.

jbhoot commented 2 years ago

Tracking the issue at https://youtrack.jetbrains.com/issue/WEB-57226/Type-sometimes-isnt-inferred-correctly-The-type-hint-shows-the-structure-of-the-type-instead-of-its-name