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

Cannot invoke an object which is possibly 'undefined' #559

Closed
ghost opened this issue Nov 25, 2020 · 1 comment · Fixed by #561
Closed

Cannot invoke an object which is possibly 'undefined' #559

ghost opened this issue Nov 25, 2020 · 1 comment · Fixed by #561

Comments

@ghost
Copy link

ghost commented Nov 25, 2020

I'm not sure if this is intended, but the .rebuild() method from the BuildResult interface, accordingly to the snippet below, can also be undefined:

rebuild?: (() => Promise<BuildResult>) & { dispose(): void }; // Only when "incremental" is true

Because of this, is necessary to adhere to some hacky solutions like this:

const buildResult = await esbuildService.build({ ...esbuildConfig, incremental: true })
buildResult.rebuild!() // non-null assertions defeats the purpose of using "strict: true" on tsconfig.json

or also:

const buildResult = await esbuildService.build({ ...esbuildConfig, incremental: true })
buildResult.rebuild && buildResult.rebuild()

or even this:

const buildResult = await esbuildService.build({ ...esbuildConfig, incremental: true })
buildResult.rebuild?.call(this)

Is there anything that i'm missing? as shown in the example from 0.8.12 release https://github.com/evanw/esbuild/blob/master/CHANGELOG.md), there were no need for any checking to use the .rebuild() method

require('esbuild').build({
  entryPoints: ['app.js'],
  bundle: true,
  outfile: 'out.js',
  incremental: true,
}).then(result => {
  // The "rebuild" method is present if "incremental" is true. It returns a
  // promise that resolves to the same kind of object that "build" returns.
  // You can call "rebuild" as many times as you like.
  result.rebuild().then(result2 => {
    // Call "dispose" when you're done to free up resources.
    result.rebuild.dispose()
  })
})
@nettybun
Copy link

nettybun commented Nov 25, 2020

It says

"The rebuild method is present if "incremental" is true

So that's why it's undefined. You might be able to do some TypeScript hackery to return a version of BuildResult with that one property added as non-optional, but I don't know if it's worth it since ! works. It's also not the only field that has conditional behaviour like this, see "outputFiles" on the same interface.

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

Successfully merging a pull request may close this issue.

1 participant