Skip to content

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

@ghost

Description

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()
  })
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions