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

define generic type within generic type? #2

Open
unional opened this issue Sep 13, 2020 · 0 comments
Open

define generic type within generic type? #2

unional opened this issue Sep 13, 2020 · 0 comments

Comments

@unional
Copy link
Contributor

unional commented Sep 13, 2020

Problem

How to generalize this?

export type Number<Value extends number = number> = { name: 'number', value: Value }
type val = <Value extends number>(value: Value) => Number<Value> 

Currently this is not possible:

type Val<T, A> = <Value extends A>(value: Value) => T<A>
                                                    ^ T is not generic

This is not possible in TypeScript because type is evaluated onsite.
And also there is no syntax for this. Something like

type Val<T<R>, A, infer R extends A> = <Value extends R>(value: Value) => T<R>

type Val<T<V extends A>, A> = (value: V) => T<V>

but can't find a good syntax in usage:

const val = <Value extends number>(value: Value) => ({ name: 'number', value }) as Number<Value>
const val = <Value extends number>(value: Value): Number<Value> => ({ name: 'number', value })
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

1 participant