Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.

Support for Existential Type (*) #186

Open
thecotne opened this issue Apr 29, 2020 · 5 comments
Open

Support for Existential Type (*) #186

thecotne opened this issue Apr 29, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@thecotne
Copy link
Contributor

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'
})
@JSMonk JSMonk added the enhancement New feature or request label Apr 29, 2020
@JSMonk
Copy link
Owner

JSMonk commented Apr 29, 2020

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

@amatiasq
Copy link

amatiasq commented Dec 3, 2020

This feature is called as const in TS:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions

@thecotne
Copy link
Contributor Author

thecotne commented Dec 3, 2020

@amatiasq not true

"Existential Type" is completely different feature

as const is hint for inference system to match type to value as close as possible so "Filtering" is "Filtering" not string

"Existential Type" is way of writing incomplete type definition that acts as constraints for inference system

@amatiasq
Copy link

amatiasq commented Dec 3, 2020

I'm sorry I don't get what's Existential Type then. I got confused by the original post where:

const FEATURES: $Immutable<{
    FILTERING: 'Filtering',
    REORDERING: 'Reordering',
    SORTING: 'Sorting'
}> = {
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering'
}

and

const FEATURES: $Immutable<*> = {
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering'
}

Look to me like

const FEATURES = {
  SORTING: 'Sorting',
  FILTERING: 'Filtering',
  REORDERING: 'Reordering'
} as const

I was looking for constant assertions when I found this issue should I create a different one then?

@thecotne
Copy link
Contributor Author

thecotne commented 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants