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

Type Params #4

Closed
sledorze opened this issue Aug 21, 2017 · 5 comments
Closed

Type Params #4

sledorze opened this issue Aug 21, 2017 · 5 comments

Comments

@sledorze
Copy link
Contributor

We cannot express the code below currently (because of Type Params):

type Success<T> = { type: 'success', value: T }
const makeSuccess = <T>(value: T): Result<T> => ({ type: 'success', value });
const isSuccess = <T>(r: Result<T>): r is Success<T> => r.type === 'success';

type Failure = { type: 'failure' }
const makeFailure = <T>(): Result<T> => ({ type: 'failure' });
const isFailure = <T>(r: Result<T>): r is Failure => r.type === 'failure';

type Result<T> = Success<T> | Failure;

It may not be possible with the current API (Not spent any time thinking about it).
However for the record, let put that here.

@pelotom
Copy link
Owner

pelotom commented Aug 21, 2017

Yeah, I'm not sure how to encode this sort of polymorphism in the input record type. Definitely something to think about...

@sledorze
Copy link
Contributor Author

sledorze commented Sep 29, 2017

@pelotom For information; I've used the HKT trick of fp-ts and came to a working solution

export interface Foo {
    name: string
}
export interface Bar<T> {
    provider: All<T>,
    params: T
}

export interface Baz<T> {
    queries: All<T>[]
}


export type Sum<T> = {
    foo: Foo,
    bar: Bar<T>,
    baz: Baz<T>
}

export type All<T> = SumOf<Sum<T>>

declare module './SumType' {
    interface SumType<T> {
        sumTest: Sum<T>
    }
}

const query =
    sumizeHK('sumTest')({
        foo: undefined,
        bar: undefined,
        baz: undefined
    })

@sledorze
Copy link
Contributor Author

no need to keep that open..

@sledorze
Copy link
Contributor Author

@pelotom do you think an encoding in the input record with the technic you've used here https://github.com/pelotom/hkts could now be possible?

@pelotom
Copy link
Owner

pelotom commented Sep 16, 2018

You could construct unions that involve placeholders types (_) and then... I'm not sure. It seems like it would be a pain to work with, forcing client code to explicitly use the substitution operator $ everywhere. I think before this can become practical we need something like polymorphic values (microsoft/TypeScript#17574). Otherwise it's impossible for the entire type returned by unionize to be parameterized by some type... we're limited to returning polymorphic functions.

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

No branches or pull requests

2 participants