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

Callback type: Make value optional, make error nullable #342

Merged
merged 1 commit into from
Apr 15, 2021

Conversation

thomastoye
Copy link
Contributor

Without this, code like the following will fail to compile with strictNullChecks on:

import avsc from 'avsc'
import crc32 from 'buffer-crc32'
import snappy from 'snappy'

export const encodeAvro = (schema: avsc.Schema) =>
  new avsc.streams.BlockEncoder(schema, {
    writeHeader: 'auto',
    codec: 'snappy',
    codecs: {
      snappy: (buf, cb) => {
        const checksum = crc32(buf)
        snappy.compress(buf, (err, deflated) => {
          if (err) {
            return cb(err) // <---
          }

          const block = Buffer.alloc(deflated.length + 4)
          deflated.copy(block)
          checksum.copy(block, deflated.length)
          cb(null, block)
        })
      }
    }
  })

The compiler will complain that no argument is given for value in cb(err).

Example from a callback type in @types/node for reference: type TransformCallback = (error?: Error | null, data?: any) => void;

@coveralls
Copy link

Coverage Status

Coverage remained the same at 99.676% when pulling 6c08569 on thomastoye:patch-1 into ac8d040 on mtth:master.

@mtth mtth merged commit e11bdfa into mtth:master Apr 15, 2021
@mtth
Copy link
Owner

mtth commented Apr 15, 2021

Thanks for the catch and fix @thomastoye.

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 this pull request may close these issues.

3 participants