Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [Typescript] eslint rule to avoid some common gotchas #7402

Open
Xiot opened this issue May 17, 2024 · 4 comments
Open

✨ [Typescript] eslint rule to avoid some common gotchas #7402

Xiot opened this issue May 17, 2024 · 4 comments

Comments

@Xiot
Copy link

Xiot commented May 17, 2024

What are you trying to do?

While trying to get around #7401, I tried to keep the @field as a string, but then just type a custom property.

@object()
export class MergeRequestReference {
  @field() _iid: string
  @field() _type: string

  constructor(iid: string, type: 'head' | 'merge') {
    this._iid = iid
    this._type = type
  }

  get iid() {
    return this._iid
  }
  get type() {
    return this._type
  }
}

However it seems that the prototype isn't actually preserved when the object is used from another module.

First prize, would be to re-attach the prototype chain, but barring that, it would be nice to have an eslint rule to catch things that work slightly differently than a typescript developer would expect.

Why is this important to you?

It would avoid some errors caused by the way that dagger modules are executed.

How are you currently working around this?

I just removed the custom getters, and I check the types later in the code

@TomChv TomChv added the sdk/typescript All about TypeScript sdk label May 22, 2024
@TomChv
Copy link
Member

TomChv commented May 22, 2024

@Xiot Thanks for your report!

However it seems that the prototype isn't actually preserved when the object is used from another module.

It's because we currently do not support that kind of annotation (see #7401 (comment))

First prize, would be to re-attach the prototype chain, but barring that, it would be nice to have an eslint rule to catch things that work slightly differently than a typescript developer would expect.

I'm not sure I understand the idea behind, do you mean having an eslint that can catch unsupported feature of the SDK such as custom inline union type?

@Xiot
Copy link
Author

Xiot commented May 22, 2024

Well there two main things here. Well 3 if you count the string literals, but thats for another issue.

The thing that caught me a little off guard was that the prototype chain isn't preserved when you re-hydrate a field.

@object()
class RootModule {
  @func() test() {
    return new PModule(new PCheck(4))
  }
}

@object()
class PCheck {
  @field() value: number

  constructor(value: number) {
    this.value = value
  }
  get doubled() {
    return this.value * 2
  }
}

@object()
class PModule {
  @field() value: PCheck
  constructor(value: PCheck) {
    this.value = value
  }
  @func() print() {
    console.log('value', this.value, Object.getPrototypeOf(this.value))
    return this.value.doubled ?? 0
  }
}

Calling

dagger call test print

will return 0 and will print

value { value: 4 } [Object: null prototype] {} 

That shows that prototype of the value field is set to null, so the doubled property isn't actually there.

My comment about eslint, was coming up with a way to let developers know that even though what they are doing is supported in the language, it won't actually work.

@Xiot
Copy link
Author

Xiot commented May 22, 2024

I should have actually created a bug for the prototype not being there, rather than the eslint rule

@Xiot
Copy link
Author

Xiot commented May 22, 2024

Created a separate issue for the prototype #7446

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants