#Extending a chain api in typescript

7 messages · Page 1 of 1 (latest)

wary crypt
#
comment().delete().edit("Hello :)")

comment has the method edit, and it extends a generic function which has the method delete

This causes a typescript error because once delete is used it thinks its on the generic function

west topazBOT
#
pyroteamfortresstwo#0

Preview:```ts
const generic = function () {
const api = {
_deleted: false,
delete() {
this._deleted = true
return this
},
}
return api
}

const comment = function () {
const api = {
...generic(),
_text: "",
edit(text: string) {
this._text = text
...```

kind jolt
pure cradle
west topazBOT
#
nonspicyburrito#0

Preview:```ts
class Entity {
deleted = false

public delete() {
this.deleted = true
return this
}
}

class CommentEntity extends Entity {
text = ""

public edit(text: string) {
this.text = text
return this
}
}

new CommentEntity().delete().edit("hello world")
...```

kind jolt
west topazBOT
#
mkantor#0

Preview:```ts
const generic = function () {
const api = {
_deleted: false,
delete<T extends {_deleted: boolean}>(this: T) {
this._deleted = true
return this
},
}
return api
}

const comment = function () {
const api = {
...generic(),
...```