#Narrowing template literals
5 messages · Page 1 of 1 (latest)
Preview:```ts
const LOG_LEVEL = {
ERROR: 0,
WARN: 1,
INFO: 2,
}
const createLogger = (
logLevel: keyof typeof LOG_LEVEL | ${number}
) => {
let level: keyof typeof LOG_LEVEL
if (logLevel in LOG_LEVEL) {
level = logLevel // throws error
}
...```
You can choose specific lines to embed by selecting them before copying the link.
you can do this but it might not be very satisfying:
Preview:ts ... if ( logLevel === "ERROR" || logLevel === "WARN" || logLevel === "INFO" ) { level = logLevel } ...
i don't think you can directly check for ```${number}` `` to rule out that possibility, at least not without writing a custom type guard