#Type check when explicit resource management isn't used properly?

5 messages · Page 1 of 1 (latest)

short gust
#

In the following code, I would like to arrange things so that the call to getResource in badFunction raises some kind of type checking error, because it leaks a resource that should have been captured with using. Is this possible?

function getResource(): Disposable {
  console.log("Acquired resource!")
  return {
    [Symbol.dispose]: () => {
      console.log("Freed resource")
    }
  }
}

function goodFunction() {
  using resource = getResource()
  // do work
  // resource is disposed
}

function badFunction() {
  const resource = getResource()
  // do work
  // resoruce leaks!
}
buoyant drift
#

TS's job is to prevent type error, not disposing doesn't cause a type error at runtime. What you are trying to solve is a code quality issue, which is the job of ESLint.

#

Although for now new explicitly resource management is, I'm not sure if there's a rule for it yet.

short gust
#

Good point, i think you're right, it's more of a linter warning than a typing issue, once i checked for lint rules I found this: https://github.com/typescript-eslint/typescript-eslint/issues/7160 so it looks like it will be awhile before there is a working rule

GitHub

Before You File a Proposal Please Confirm You Have Done The Following... I have searched for related issues and found none that match my proposal. I have searched the current rule list and found no...

#

!resolved