This demo makes typeof Symbol.toStringTag
type symbol
:
const promise = Promise.resolve();
const toStr: typeof Symbol.toStringTag | void = typeof Symbol === 'function' ? Symbol.toStringTag : undefined;
if (toStr) {
console.log(promise[toStr] === 'Promise');
}
And results in error:
Element implicitly has an 'any' type because expression of type 'symbol' can't be used to index type 'Promise'.(7053)
Is there a way to narrow down toStr
symbol type instead of using symbols explicitly like promise[Symbol.toStringTag]
?
Stated that I'm not getting the same error, but I got a true
printed on my console with following compilerOptions
"compilerOptions": {
"declaration": true,
"module": "commonjs",
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"target": "es6"
}
Since you know that typeof Symbol.toStringTag === "symbol"
, why not?
const promise = Promise.resolve();
const toStr: symbol | void = typeof Symbol === "function" ? Symbol.toStringTag : undefined;
if(toStr) console.log(promise[toStr] === "Promise");
Once said that I think the key is suppressImplicitAnyIndexErrors
compile option. Don't know if it is a reasonable compromise for you.
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With