Effect CommunityEC
Effect Community3y ago
17 replies
douglasm

Narrowing Types in a Pipeline

What's the best way to narrow types over a pipeline? For example, I have code like this:

  return pipe(
    s3Client.headObject.send(
      new HeadObjectCommand({
        Bucket: bucket,
        Key: prefix
      })
    ),
    Effect.flatMap(headObject => {
      return Effect.if(headObject.ContentLength === undefined, {
        onTrue: Effect.fail(new NoSuchKeyError({ error: 'No such key' })),
        onFalse: Effect.succeed(() => {
          metricsService.logFileSize(headObject.ContentLength); <--- typescript seeings headObject.ContentLegnth as being `number | undefined` despite the check above
        })
      });
    }),
Was this page helpful?