!xfqwqigUlAuUTEIPwA:matrix.org

TypeScript

1499 Members
A superset of JavaScript that compiles to clean JavaScript output. https://www.typescriptlang.org/54 Servers

Load older messages


SenderMessageTime
30 Nov 2020
@gitter_webstrand:matrix.orgwebstrand (Gitter)*builtin type-safe way19:48:30
@gitter_pyoor__twitter:matrix.orgJason Kratzer (Gitter) doesn't that seem crazy that 'in' can't be used to narrow this type? 19:50:08
@gitter_pyoor__twitter:matrix.orgJason Kratzer (Gitter) and you mean like this?
if ('value' in x[key]) {
        console.log((x[key] as { value: string}).value)
    }
19:50:16
@gitter_pyoor__twitter:matrix.orgJason Kratzer (Gitter) (edited) ... { console.log((x[key] as { value: string}).value) } ``` => ... { console.log((x[key] as { value: string}).value) } ``` 19:50:22
@gitter_webstrand:matrix.orgwebstrand (Gitter)yeah. I understand there are some technical difficulties that make this kind of narrowing difficult, or at least difficult to do with reasonable performance19:50:58
@gitter_webstrand:matrix.orgwebstrand (Gitter) alternatively console.log((x[key] as typeof x['foo']).value) 19:51:51
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) joined the room.20:16:26
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)Hi. Has anybody ever tried creating a mixin to add an overload to an existing interface?20:16:27
@gitter_webstrand:matrix.orgwebstrand (Gitter) Yeah, I've done that 20:16:47
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) I'm trying to add an overload to EventTarget.AddEventListener that takes an event class instead of a string. 20:17:03
@gitter_webstrand:matrix.orgwebstrand (Gitter) Oh, that kind of mixin.
You can use declaration merging to add onto existing interfaces
20:17:53
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) Maybe I'm overthinking it, but I'd like to avoid the magic strings in Add/RemoveEventListener methods. 20:18:11
@gitter_webstrand:matrix.orgwebstrand (Gitter)Is EvenTarget the DOM one or from Node.js? I can never remember20:18:46
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)DOM20:18:50
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)I want to apply the mixin to multiple classes and have the mixin call the original implementation from each class.20:19:53
* @gitter_resynth1943:matrix.orgResynth (Gitter) is skeptical: be careful when extending native interfaces. 20:20:17
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) Yeah, after struggling with it for an hour or so, I'm skeptical too. 20:20:37
@gitter_webstrand:matrix.orgwebstrand (Gitter) What he's doing should be fine 20:20:43
@gitter_webstrand:matrix.orgwebstrand (Gitter)Do you have any example code I can use as a basis?20:20:54
@gitter_resynth1943:matrix.orgResynth (Gitter) BTW magic strings are hella useful for strongly typed event emitters, @InstantKafka_twitter 20:21:01
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)how so?20:21:32
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) '''
addEventListener(type: { name: string }, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void {
(this as EventTarget).addEventListener(stringOrClassName(type), listener, options);
}
'''
20:25:54
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)hrmm...20:25:59
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) (edited) hrmm... => ``` addEventListener(type: { name: string }, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void { (this as EventTarget).addEventListener(stringOrClassName(type), listener, options); } ``` 20:26:10
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter) stringOrClassName() returns the value of the .name property if it exists or the value if it's a string. 20:27:00
@gitter_instantkafka_twitter:matrix.orgInstant Kafka (Gitter)And another overload that takes a string or { name: string } for the type.20:29:08
@gitter_webstrand:matrix.orgwebstrand (Gitter) for instance
class PotatoGun extends triggerMixin(EventTarget) {

}

function triggerMixin<T extends new (...args: any[]) => EventTarget>(cons: T) {
    return class trigger extends cons {
        addEventListener(type: "fire", listener: (this: PotatoGun, ev: ["fireEvent"]) => any, options?: boolean | AddEventListenerOptions): void;
        addEventListener(type: string, listener: (this: PotatoGun, ...args: any[]) => any, options?: boolean | AddEventListenerOptions): void;
        addEventListener(type: string, listener: (this: PotatoGun, ...args: any[]) => any, options?: boolean | AddEventListenerOptions): void {
            super.addEventListener(type, listener, options);
        }
    }
}

declare let instance: PotatoGun;

instance.addEventListener("fire", (ev) => {});
20:29:24
@gitter_webstrand:matrix.orgwebstrand (Gitter) if you don't add the type: string overload, your class becomes difficult to extend with new events 20:30:07
@gitter_webstrand:matrix.orgwebstrand (Gitter)(for instance, a subclass will have a difficult time adding new events)20:30:25
@gitter_webstrand:matrix.orgwebstrand (Gitter) All that being said, I've gone down the path of using EventTarget, and I gave up and implemented my own event subsystem. 20:31:07

There are no newer messages yet.


Back to Room List