Skip to content

TypeScript error when trying to configure optional sinks #75

Closed

Description

@rojakcoder

I'm trying to create conditional configurations for LogTape based on environment variables but keep bumping up against the TypeScript error about types being incompatible.

This is the closest that I've got.

  let sinks:
    | Record<'console' | 'syslog', Sink>
    | Record<'console' | 'syslog' | 'file', Sink> = {
    console: getConsoleSink(),
    syslog: getSyslogSink(),
  };
  let loggers = [
    {
      category: ['logtape', 'meta'],
      sinks: ['syslog'],
    },
    {
      category: appName,
      lowestLevel: 'debug',
      sinks: ['console'],
    },
  ]
  if (logFile) {
    sinks = {
      ...sinks,
      file: getFileSink(`${logDir}/${logFile}`),
    };
    loggers = [
      loggers[0],
      {
        category: appName,
        lowestLevel: 'debug',
        sinks: ['console', 'file'],
      }
    ]
  }
  await configure({
    sinks,
    loggers,
 // ^^^^^^^ Complains about "Type 'string' is not assignable to type '"console" | "syslog"'."
  });

Is there a way around this? Or is the only option to call await configure in an if-else statement? i.e.

if (logFile) {
  await configure(...); // Invoke with file sink.
} else {
  await configure(...); // Invoke without file sink.
}

Activity

  1. dahlia commented on Jul 22, 2025

    @dahlia
    Owner

    How about this?

    await configure({
      sinks: {
    	console: getConsoleSink(),
    	syslog: getSyslogSink(),
        file: logFile
          ? getFileSink(`${logDir}/${logFile}`)
          : (_: LogRecord) => {},
      },
      loggers: [
        {
          category: ['logtape', 'meta'],
          sinks: ['syslog'],
        },
        {
          category: appName,
          lowestLevel: 'debug',
          sinks: ['console', 'file'],
        },
      ],
    });
  2. rojakcoder commented on Jul 22, 2025

    @rojakcoder
    Author

    That works! The gist is to create a placeholder function for the key that is added later.

  3. Repository owner locked and limited conversation to collaborators on Jul 23, 2025
  4. converted this issue into a discussion #76 on Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions