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

generic typeguard do not work as filter #811

Closed
Lonli-Lokli opened this issue Nov 9, 2022 · 3 comments
Closed

generic typeguard do not work as filter #811

Lonli-Lokli opened this issue Nov 9, 2022 · 3 comments
Labels
invalid This doesn't seem right typings Typescript public type definitions issues

Comments

@Lonli-Lokli
Copy link

Lonli-Lokli commented Nov 9, 2022

What is the current behavior:
https://tsplay.dev/wXq5JN

import {sample, createEvent, createEffect, createStore} from "effector";

type Doggy<D> = {
    readonly _tag: 'Dog';
    readonly barkingTime: D;
}
type Catty<C> = {
    readonly _tag: 'Cat';
    readonly milkSounds: C;
}
type Pet<D, C> = Doggy<D> | Catty<C>;


const SmallCat: Catty<string> = {
    _tag: 'Cat',
    milkSounds: 'mew'
}

const whenFeedCat = createEvent<string>();
const $activePet = createStore<Pet<number, string>>(SmallCat);
const printMilkFx = createEffect(({milk, time}: {
    milk: string,
    time: string
}) => {
    console.log(`Feed at ${time} when milk is ${milk}`);
});



sample({
    clock: whenFeedCat,
    source: $activePet,
    filter: isCat,
    fn: (cat, time) => ({milk: cat.milkSounds, time}), // TS error
    target: printMilkFx
})

sample({
    clock: whenFeedCat,
    source: $activePet,
    filter: (pet: Pet<number, string>) => isCat(pet),
    fn: (cat, time) => ({milk: cat.milkSounds, time}), // TS error
    target: printMilkFx
})

sample({
    clock: whenFeedCat,
    source: $activePet,
    filter: pet => isCat(pet),
    fn: (cat, time) => ({milk: cat.milkSounds, time}), // TS error
    target: printMilkFx
})

function isCat<A>(pet: Pet<any, A>): pet is Catty<A> {
    return pet._tag === 'Cat';
}

What is the expected behavior:
Proper types

Which versions of effector packages, and which browser and OS are affected by this issue? Did this work in previous versions of effector?:
latest

@Lonli-Lokli Lonli-Lokli added bug Something isn't working needs triage labels Nov 9, 2022
@sergeysova sergeysova added the typings Typescript public type definitions issues label Nov 9, 2022
@Lonli-Lokli
Copy link
Author

I have one more case when typescript does not report an error with the sample (link)

import {sample, createEvent, createStore} from "effector";

type AnyObject = {
    name: string;
}
function isDefined<T>(value: T | null | undefined): value is T {
    return  value !== null && value !== undefined;
}

const voidEvent = createEvent();
const stringEvent = createEvent<string>();
const $nullableStore = createStore<AnyObject | null>(null);

sample({
  clock: voidEvent,
  source: $nullableStore,
  filter: isDefined,
  target: stringEvent
});

@zerobias zerobias closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2023
@zerobias zerobias added invalid This doesn't seem right and removed bug Something isn't working needs triage labels Apr 15, 2023
@sergeysova
Copy link
Collaborator

sergeysova commented Apr 15, 2023

@zerobias But isCat is a type-guard:

sample({
    clock: whenFeedCat,
    source: $activePet,
    filter: isCat,
    fn: (cat, time) => ({milk: cat.milkSounds, time}), // TS error
    target: printMilkFx
})

// ***

function isCat<A>(pet: Pet<any, A>): pet is Catty<A> {
    return pet._tag === 'Cat';
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right typings Typescript public type definitions issues
Projects
None yet
Development

No branches or pull requests

3 participants