Repository navigation
TypeScript error when trying to configure optional sinks #76
Answered
by
dahlia
rojakcoder
asked this question in
Q&A
|
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 if (logFile) {
await configure(...); // Invoke with file sink.
} else {
await configure(...); // Invoke without file sink.
} |
Answered by
dahlia
Jul 22, 2025
Replies: 2 comments
|
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'],
},
],
}); |
0 replies
Answer selected by
dahlia
|
That works! The gist is to create a placeholder function for the key that is added later. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about this?