JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.1k stars 59 forks source link

Support computed property name #144

Open artalar opened 4 years ago

artalar commented 4 years ago

Currently is imposible to describe computed property name, thats is incompatible with ES

https://hegel.js.org/try#MYewdgzgLgBADgJxHApgqBPAcgQwLYowC8MA5IsmprgaQFABmArmMFAJbgzAIo5QoA8gCMAVgAoA1igwAaeEjgBKGAG86MTTF5QmCMGo1bjAbWkYAugC4FyI5oC+dJ3VCRYIMcW69+QseIUqOjY+CjyACwATEp0cW7QMOYAajgANkyEJJ6iAHTmruCJQVShBKkZWTA5uSUhNChAA

image

thecotne commented 4 years ago

@JSMonk how would you type createObj manually?

this does not work

const propertyName = 'propertyName'

function createObj<_a: number | string | symbol, _b>(key: _a, prop: _b): { _a: _b } {
    return {
        [key]: prop
    }
}

const obj = createObj(propertyName, 42)

const keyValue = obj.key
const propertyNameValue = obj.propertyName
JSMonk commented 4 years ago

@JSMonk how would you type createObj manually?

this does not work

const propertyName = 'propertyName'

function createObj<_a: number | string | symbol, _b>(key: _a, prop: _b): { _a: _b } {
    return {
        [key]: prop
    }
}

const obj = createObj(propertyName, 42)

const keyValue = obj.key
const propertyNameValue = obj.propertyName

Good question. I will add computed property inside the type syntax soon.

thecotne commented 4 years ago

WhAT tHe TrIn?

const foo: string = 'foo'
const bar = 'bar'

function createObj(key, prop) {
    return {
        [key]: prop
    }
}

const objWithFoo = createObj(foo, 42)
const objWithBar = createObj(bar, 44)

const result = objWithFoo.trin + objWithBar.bar;

try

JSMonk commented 4 years ago

WhAT tHe TrIn?

const foo: string = 'foo'
const bar = 'bar'

function createObj(key, prop) {
    return {
        [key]: prop
    }
}

const objWithFoo = createObj(foo, 42)
const objWithBar = createObj(bar, 44)

const result = objWithFoo.trin + objWithBar.bar;

try

Lol, will fix it today :D