question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Existential Type (*)

See original GitHub issue

supporting 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:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
JSMonkcommented, Apr 29, 2020

Looks good. But I want to add it as _ (underscore) syntax. Thank you for contribution ^_^.

1reaction
thecotnecommented, Dec 3, 2020

@amatiasq this issue touches on same use case as as const but it’s more complex feature then as const so i think it’s ok to create different issue for as const and link to this issue as possible solution

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found