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

Better forward typings (TS) #229

Merged
merged 6 commits into from Oct 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/effector/index.d.ts
Expand Up @@ -260,7 +260,31 @@ export const step: {
update(data: {store: StateRef}): Update
run(data: {fn: (data: any, scope: {[field: string]: any}) => any}): Run
}
export function forward<T>(opts: {from: Unit<T>; to: Unit<T>}): Subscription

// Allow `* -> void` forwarding (e.g. `string -> void`).
export function forward(opts: {
from: Unit<any>
to: Unit<void>
}): Subscription
export function forward<T>(opts: {
/**
* By default TS picks "best common type" `T` between `from` and `to` arguments.
* This lets us forward from `string | number` to `string` for instance, and
* this is wrong.
*
* Fortunately we have a way to disable such behavior. By adding `& {}` to some
* generic type we tell TS "do not try to infer this generic type from
* corresponding argument type".
*
* Generic `T` won't be inferred from `from` any more. Forwarding from "less
* strict" to "more strict" will produce an error as expected.
*
* @see https://www.typescriptlang.org/docs/handbook/type-inference.html#best-common-type
*/
from: Unit<T & {}>;
to: Unit<T>
}): Subscription
// Do not remove the signature below to avoid breaking change!
export function forward<To, From extends To>(opts: {
from: Unit<From>
to: Unit<To>
Expand Down
76 changes: 71 additions & 5 deletions src/types/__tests__/effector/forward.test.js
Expand Up @@ -128,7 +128,10 @@ describe('forward with subtyping', () => {
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors
Type 'Event<string | number>' is not assignable to type 'Unit<string>'.
Types of property '__' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.

--flow--
Cannot call 'forward' with object literal bound to 'opts'
Expand Down Expand Up @@ -170,11 +173,11 @@ describe('forward with subtyping', () => {
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
Type 'Event<string | number>' is not assignable to type 'Unit<string>'.
Type 'Event<string | number>' is not assignable to type 'Unit<string & {}>'.
Types of property '__' are incompatible.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.

Type 'string | number' is not assignable to type 'string & {}'.
Type 'number' is not assignable to type 'string & {}'.
Type 'number' is not assignable to type 'string'.

--flow--
Cannot call 'forward' with object literal bound to 'opts'
Expand All @@ -190,4 +193,67 @@ describe('forward with subtyping', () => {
"
`)
})
it('generics `to` and `from` (should pass)', () => {
forward<string | number, string>({to: strOrNum, from: str})
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors

--flow--
no errors
"
`)
})
it('generics `to` and `from` (should fail on providing generics)', () => {
forward<string, string | number>({to: str, from: strOrNum})
expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
Type 'string | number' does not satisfy the constraint 'string'.
Type 'number' is not assignable to type 'string'.


--flow--
Cannot call 'forward' with object literal bound to 'opts'
forward<string, string | number>({to: str, from: strOrNum})
^^^^^^^^^^^^^^^^^^^^^^^^^
number [1] is incompatible with string [2] in type argument 'T' [3] of property 'from'
const strOrNum: Event<string | number> = createEvent()
[1] ^^^^^^
forward<string, string | number>({to: str, from: strOrNum})
[2] ^^^^^^
interface CovariantUnit<+T> {
[3] ^
"
`)
})
})

describe('better inference experience', () => {
it('should forward from `Unit<*>` to `Unit<void>`', () => {
const from = createEvent<string>()
const to = createEvent<void>()

forward({from, to})

expect(typecheck).toMatchInlineSnapshot(`
"
--typescript--
no errors

--flow--
Cannot call 'forward' with object literal bound to 'opts'
forward({from, to})
^^^^^^^^^^
undefined [1] is incompatible with string [2] in type argument 'T' [3] of property 'to'
const to = createEvent<void>()
[1] ^^^^
const from = createEvent<string>()
[2] ^^^^^^
export interface Unit<T> extends CovariantUnit<T>, ContravariantUnit<T> {
[3] ^
"
`)
})
})