#Initializer provides no value for this binding element and the binding element has no default value

11 messages · Page 1 of 1 (latest)

tawdry dove
#

I have a JavaScript function like this:

const FETCH_DATA = {
  readAsArrayBuffer(axios, url, signal, { onDownloadProgress } = {}) {
    console.log('ReaderFactory - FETCH_DATA - readAsArrayBuffer: Running');
    return axios
      .get(url, {
        responseType: 'arraybuffer',
        signal,
        onDownloadProgress,
      })
      .then(({ data }) => data);
  },
};

When I convert the file to TS I get the error:

TS2525: Initializer provides no value for this binding element and the binding element has no default value.

This is being thrown by onDownloadProgress but I am unsure how to resolve this. Can anyone assist? TIA!

fathom finchBOT
#

:warning: Please wait a bit longer. You can ping helpers <t:1679504739:R>.

coral nymph
#

@tawdry dove You should define types for your params.

#

TS is saying it cannot determine what type onDownloadProgress is supposed to be.

tawdry dove
#

Yeah, but how does one declare a type on a property that is being destructured? That seems to be the issue

coral nymph
#

You generally define a type for the whole object being destructured

#
type ReadAsArrayBufferOptions = Partial<{
  onDownloadProgress: (/* probably an event type here */) => void;
}
const FETCH_DATA = {
  readAsArrayBuffer(axios: AxiosType, url: string, signal: SignalType, { onDownloadProgress }: ReadAsArrayBufferOptions = {}) {
tawdry dove
#

Ahh, that makes sense but I can use partial in the meantime as you have above?

coral nymph
#

I'm not sure what you mean? The point of Partial here is just because all the options are apparently optional.

tawdry dove
#

Ahh, okay. I think I understand now, thanks!

coral nymph
#

!resolved