i like to create my own console logs.
//-- Creating own console logs for better logging
console.detailed = function(payload) {
return console.log(util.inspect(payload, { showHidden: false, depth: null }))
}
console.notice = function(payload) {
return console.log('\x1b[33m%s\x1b[0m', payload)
}
I just started with typescript and here I am getting error that
Property 'detailed' does not exist on type 'Console'
or
Property 'notice' does not exist on type 'Console'.
Can anyone please help me in solving the above error?
Update: From the answer by Saravana, Can someone explain me this as well in more human terms
In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module. Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well)." If your code is inside a module, you need to wrap it inside global
And
You might also need to wrap this inside the global scope if you are using this inside a module. See https://www.typescriptlang.org/docs/handbook/declaration-merging.html#global-augmentation
You have to extend the Console
interface to add the new method:
interface Console {
detailed: (payload: any) => void
}
console.detailed("works");
Note that if your file is a module (i.e. it contains import
or export
statements), then you have to declare this in the global scope for this to work:
For example, if your file is a module:
import * as moment from "moment"; // This makes this file a module
declare global {
interface Console {
detailed: (payload: any) => void
}
}
// Your actual method definition
console.detailed = (payload) => {
console.log("Timestamp:", moment().unix());
console.log(payload);
}
// Usage
console.detailed("works");
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加