Skip to content

Conversation

@ivanross
Copy link

@ivanross ivanross commented May 1, 2020

This adds type definitions to traph function.

It is possible to have type checking for the object passed to traph function by passing two types: the expected input object type and the expected output object type.

type In = { values: number[] }
type Out = { count: number , mean: number }

traph<In,Out>({
	count: (i, o) => i.values.length
	mean: (i, o) => i.values.reduce(sum) / o.count
})

In each callback i type is In and o type is Out.

Also, the argument passed to traph must have all and only the keys of Out, where every key must have proper return type:

traph<In,Out>({
	square: (i, o) => ... // Error: square in not assignable 
	count: (i, o) => String(i.values) // Error: count type must be 'number'
})

This PR adds tsd test too.

@caesarsol
Copy link
Owner

Wow thanks!!!

Is it impossible to infer output types though?
We would lose the o typing, right?

@@ -0,0 +1,23 @@
import { expectType, expectAssignable, expectError } from "tsd"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my god, I missed this library completely... I'm losing my edge

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤯

"test:watch": "ava --watch",
"test": "npm run lint && ava",
"prepublish": "npm run build && npm test"
"test:types": "tsd",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we call both ava and tsd with the yarn test and yarn test:watch scripts?

Copy link
Author

@ivanross ivanross May 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I get what you mean. Are you wondering how to call both ava and tsd in watch mode at the same time with one call? However, a "watch" option seems to be missing in tsd

@ivanross
Copy link
Author

ivanross commented May 1, 2020

I'm afraid so. I think that circular reference is unavoidable. I've tried to infer Output type from traph argument, but as you say we lose o typing.

With these types, the output is inferred when getters use only i parameter. For instance, if you look at the first test:

const transform = traph({
	fullName: i => `${i.first} ${i.last}`,
    initials: i => `${i.first[0].toUpperCase()}.${i.last[0].toUpperCase()}.`,
})

const output = transform({ first: 'Richard', last: 'Feynman' })

output is inferred to be { fullName: string, initials: string }. But as soon as you add o to one getter, even if it is not used, typescript fallbacks to Record<string,any>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants