Support for Existential Type (*)
See original GitHub issuesupporting Existential Type will cut down boilerplate code that sometimes is needed to achieve same result for example
this
const FEATURES: $Immutable<{
FILTERING: 'Filtering',
REORDERING: 'Reordering',
SORTING: 'Sorting'
}> = {
SORTING: 'Sorting',
FILTERING: 'Filtering',
REORDERING: 'Reordering'
}
type Feature = $Values<$TypeOf<FEATURES>>
const features: Array<Feature> = []
void features.push('Filtering') // ok
void features.push(FEATURES.FILTERING) // ok
void features.push('foo') // error as expected
can be simplified to this
note: usage of FEATURES
is same so i removed it in following snippets
const FEATURES: $Immutable<*> = {
SORTING: 'Sorting',
FILTERING: 'Filtering',
REORDERING: 'Reordering'
}
so we say it will be Immutable but don’t need to describe all properties and there values
there is two workarounds for this
one is to use Object.freeze
and freeze object on runtime as well
const FEATURES = Object.freeze({
SORTING: 'Sorting',
FILTERING: 'Filtering',
REORDERING: 'Reordering'
})
second is to create helper immutableId
function that will refine type but don’t change value for runtime
const immutableId = <T>(v: T): $Immutable<T> => v
const FEATURES = immutableId({
SORTING: 'Sorting',
FILTERING: 'Filtering',
REORDERING: 'Reordering'
})
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Existential type? · Issue #14466 · microsoft/TypeScript - GitHub
The main question I had was this: what is type<T> T equivalent to? The existential is only (alpha) equivalent type<T> T . You...
Read more >What is an existential type? - Stack Overflow
A universal type exists for all values of the type parameter(s). An existential type exists only for values ...
Read more >Generalized opaque and existential type constraints
Constraints on opaque and existential types are currently limited to protocol and protocol composition requirements on the underlying type.
Read more >Existential types in Rust - Adelblog
An overview of existential types. Many languages support universally quantified types, more commonly known as generics or parameterized ...
Read more >What's a usage pattern for existential type variables? - Learning
And there is fact many similar way to constraint existential types, either using type constructors: if you have an 'a. 'a t ,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Looks good. But I want to add it as
_
(underscore) syntax. Thank you for contribution ^_^.@amatiasq this issue touches on same use case as
as const
but it’s more complex feature thenas const
so i think it’s ok to create different issue foras const
and link to this issue as possible solution