Property 'detailed' does not exist on type 'Console'

anny123

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

Saravana

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]

編集
0

コメントを追加

0

関連記事

分類Dev

'Property does not exist on type 'never'

分類Dev

Property 'xxx' does not exist on type '{}'

分類Dev

Property 'values' does not exist on type 'ObjectConstructor'

分類Dev

Property 'values' does not exist on type 'ObjectConstructor'

分類Dev

property then does not exist on type void , A typescript error

分類Dev

TSLint: Property 'params' does not exist on type 'NavigationState'

分類Dev

Property 'subscribe' does not exist on type 'Subscription

分類Dev

Property 'entries' does not exist on type ObjectConstructor

分類Dev

Property 'innerText' does not exist on type 'Element'

分類Dev

Property 'VAR_PLURAL' does not exist on type

分類Dev

Typescript: Property 'DB' does not exist on type 'Global'

分類Dev

Typescript property does not exist on union type

分類Dev

Property 'store' does not exist on type 'Readonly<AppInitialProps

分類Dev

Generic property 'enabled' does not exist on type 'Node'?

分類Dev

How to define a property to avoid: Property 'X' does not exist on type 'Y'

分類Dev

How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

分類Dev

Angular 5 to 6 Upgrade: Property 'map' does not exist on type Observable

分類Dev

how to fix property does not exist on type 'Object' in ionic 3?

分類Dev

useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

分類Dev

Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable'

分類Dev

Angular 5 : Property 'then' does not exist on type 'Observable<any>'

分類Dev

Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'

分類Dev

Typescript - property scan does not exist on type Subject...?

分類Dev

Property 'filter' does not exist on type 'Object'. When trying to filter response

分類Dev

ES6: Property 'selected' does not exist on type 'Object'

分類Dev

Property 'controls' does not exist on type 'AbstractControl'. in angular 8

分類Dev

Property 'background' does not exist on type '{}' - React Router modal

分類Dev

Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'

分類Dev

Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'

Related 関連記事

  1. 1

    'Property does not exist on type 'never'

  2. 2

    Property 'xxx' does not exist on type '{}'

  3. 3

    Property 'values' does not exist on type 'ObjectConstructor'

  4. 4

    Property 'values' does not exist on type 'ObjectConstructor'

  5. 5

    property then does not exist on type void , A typescript error

  6. 6

    TSLint: Property 'params' does not exist on type 'NavigationState'

  7. 7

    Property 'subscribe' does not exist on type 'Subscription

  8. 8

    Property 'entries' does not exist on type ObjectConstructor

  9. 9

    Property 'innerText' does not exist on type 'Element'

  10. 10

    Property 'VAR_PLURAL' does not exist on type

  11. 11

    Typescript: Property 'DB' does not exist on type 'Global'

  12. 12

    Typescript property does not exist on union type

  13. 13

    Property 'store' does not exist on type 'Readonly<AppInitialProps

  14. 14

    Generic property 'enabled' does not exist on type 'Node'?

  15. 15

    How to define a property to avoid: Property 'X' does not exist on type 'Y'

  16. 16

    How to prevent "Property '...' does not exist on type 'Global'" with jsdom and typescript?

  17. 17

    Angular 5 to 6 Upgrade: Property 'map' does not exist on type Observable

  18. 18

    how to fix property does not exist on type 'Object' in ionic 3?

  19. 19

    useRef Typescript error: Property 'current' does not exist on type 'HTMLElement'

  20. 20

    Angular 6 : Property 'fromEvent' does not exist on type 'typeof Observable'

  21. 21

    Angular 5 : Property 'then' does not exist on type 'Observable<any>'

  22. 22

    Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>'

  23. 23

    Typescript - property scan does not exist on type Subject...?

  24. 24

    Property 'filter' does not exist on type 'Object'. When trying to filter response

  25. 25

    ES6: Property 'selected' does not exist on type 'Object'

  26. 26

    Property 'controls' does not exist on type 'AbstractControl'. in angular 8

  27. 27

    Property 'background' does not exist on type '{}' - React Router modal

  28. 28

    Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'

  29. 29

    Property 'setUser' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'

ホットタグ

アーカイブ