Declaring globals isn't that hard

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024
  • Become a TypeScript Wizard with my free beginners TypeScript Course:
    www.totaltypes...
    Follow Matt on Twitter
    / mattpocockuk
    Join the Discord:
    mattpocock.com...

Комментарии • 47

  • @LePhenixGD
    @LePhenixGD 8 месяцев назад +9

    Incredible insight, Matt! Declaring global types in TypeScript becomes incredibly practical, especially in scenarios like developing browser extensions, where you often use specific keywords like 'chrome' for Chromium-based browsers and 'browser' for Firefox.
    This is particularly handy when accessing features such as storage in extensions! Your explanation clears up a lot and makes it more understandable. Thanks for sharing this valuable knowledge!

    • @NoName-1337
      @NoName-1337 8 месяцев назад

      Global variables can be attackt with some special xss (if there is a vulnerability in your site). So you should avoid it.

    • @marcomow
      @marcomow 8 месяцев назад +3

      There are types for browser and chrome

  • @re.liable
    @re.liable 8 месяцев назад +1

    I did something like this in an Electron project where Node processes/functions are exposed to the UI via the global window object

  • @leokiller123able
    @leokiller123able 6 месяцев назад

    I did this with electron for communication between backend and frontend: in electron the backend exposes functions to the frontend's window, and in order to have them typed I had to do:
    declare global {
    interface Window extends MyApi {}
    }
    This typescript feature that lets you merge interfaces like this is amazing.

  • @tomorrowlimit6999
    @tomorrowlimit6999 8 месяцев назад

    my favorite use of the global scope is declaring JSX tags and Fragment from the interpreter I'm using, and then telling ESBuild to import React or whatever I'm using for jsx automatically
    so, no unnecessary obvious imports!

  • @kbsanders
    @kbsanders 8 месяцев назад

    It's important to note that this is not for implementing something in the global scope but rather only for defining the structure of something that already exists in the global scope where the implementation is defined elsewhere.

  • @JimBurch
    @JimBurch 7 месяцев назад

    Matt is the absolute typescript goat

  • @DevinWeaver
    @DevinWeaver 8 месяцев назад

    A common use case I do this with Go/WASM which typically adds a function to global. I can add the declare global in the file that calls the WASM function and everything is typed!

  • @ithinkimhipster502
    @ithinkimhipster502 8 месяцев назад +1

    This can be handy for adding type constraints on global apis like localstorage

  • @walber33
    @walber33 8 месяцев назад

    This video was very good for understanding interfaces for me, i watched some videos about types vs interfaces, and only with this video example i was really able to understand interfaces and its benefits.

  • @DevinRhode2
    @DevinRhode2 8 месяцев назад

    I like to import definitions per file. So to use the global, I need to import a certain type def

  • @YeloPartyHat
    @YeloPartyHat 8 месяцев назад

    How would you handle adding back the existing types to that new window declaration though?

  • @juanferrer5885
    @juanferrer5885 8 месяцев назад

    You also could create a types.d.ts and type Interface Window {bar: string} right?
    Im working with a js file and i have to type window with jsDoc and check with //@ts-check

  • @sumeetsood232
    @sumeetsood232 8 месяцев назад

    I get this error on using declare global in my file - "Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.":
    Error goes away if i export something from my file

    • @mattpocockuk
      @mattpocockuk  8 месяцев назад

      moduleDetection: force in tsconfig.json

  • @BobFrTube
    @BobFrTube 8 месяцев назад

    This is not entirely on topic, but I don't know how to search for it. In Express I can say
    app.use('/:page", (req,res) => {console.log(req.params.page)}; and it knows that params has a page property in that function. How does that work?
    Thanks for all your insights ...

    • @mattpocockuk
      @mattpocockuk  8 месяцев назад

      Google "augment express request object typescript"

    • @BobFrTube
      @BobFrTube 8 месяцев назад

      @@mattpocockuk Thanks. I did find paramsdictionary ... but haven't found the source of the parsing of /:id but better to continue the conversation elsewhere if you're interested.

  • @ofirsaban-p5z
    @ofirsaban-p5z 8 месяцев назад

    Thanks for you sharing!

  • @0xtz_
    @0xtz_ 8 месяцев назад +1

    😮 I tried doing this and I just ruined the types 😂 inn all project now i have a better understanding thanks 🙏

  • @proosee
    @proosee 8 месяцев назад

    What types are declared in global scope of TypeScript? Is, for example, Event/MouseEvent also declared there? I assumed it should, as this part of browser's API just like Window, but I couldn't actually found any of it in TypeScript repository.

  • @steamer2k319
    @steamer2k319 8 месяцев назад

    Would it be difficult to have e.g., zod do runtime validation of the globals? I have used it for process.env and so far it's worked well for that but I haven't tried to integrate it with the DOM window or declare global yet.

  • @malekitani3136
    @malekitani3136 8 месяцев назад

    this is interesting, thank for those useful tips

  • @AbNomal621
    @AbNomal621 8 месяцев назад

    Thank you so much.

  • @brianhunt6952
    @brianhunt6952 8 месяцев назад +1

    Amazing. Now do globalThis.

  • @code_of_honour
    @code_of_honour 8 месяцев назад

    this is common for a web3 developer

  • @Wielorybkek
    @Wielorybkek 8 месяцев назад +2

    I remember using it once for a 3rd party script that I had to use to call some external API but I gave up after some time since at some point it's become way too difficult to provide full typing. It turned out the API has also a legacy version I had to maintain, there was very little patterns between different calls, there were weird exceptions, etc. so I ended up rewriting the entire documentation of this API into my types. :D Instead I removed all the types and just pasted the documentation link in the comment. xD Job done, time saved, everyone knows how stuff works, and at the end of the day we usually anyway write some wrapper or facade for any such API so the full typing is not really necessary.

  • @bartek...
    @bartek... 8 месяцев назад

    You can't even imagine, how many time's I've trip over that problem...

  • @husnulaman
    @husnulaman 8 месяцев назад

    Thanks for this amazing tutorial. One question I have is when I type my Api responses or whatever types I have in my app, should I attach that globally or should import that type as Import type {MyType} from “..”?

  • @andresseoane9041
    @andresseoane9041 8 месяцев назад

    what about declare module as globals?
    modifying an interface in a library, as in adding fields to Express.Request so it can be used across the app

  • @Luxcium
    @Luxcium 8 месяцев назад

    _I _*_hate_*_ how those videos are so short_ as much as I love how they are straight to the point… *_No_*_ it’s not true I hate it even more for being so short…_ but it’s straight forward indeed 😅

  • @twilihgtn
    @twilihgtn 8 месяцев назад

    What about a class? Should it be declared same way in global scope to be available as is in the script?

  • @bek_shoyatbek
    @bek_shoyatbek 8 месяцев назад

    thanks for tips . Just in case , Can we use ( extend ) express-session like you said ?

  • @MirrorsEdgeGamer01
    @MirrorsEdgeGamer01 8 месяцев назад +1

    I once used *global* to add my custom HTML element names to the HTMLElement name interface so I could get autocomplete when using *createElement*. Also, this is a great video; I love your insight into these topics.

  • @yourDecisi0n
    @yourDecisi0n 8 месяцев назад +3

    This can be handy when extending global variable types like string, number, etc. You could therefore add methods to the StringConstructor or the string prototype globally

    • @DemanaJaire
      @DemanaJaire 8 месяцев назад +13

      Please, don't ever make a public library with ideas like that.

    • @yourDecisi0n
      @yourDecisi0n 8 месяцев назад +1

      @@DemanaJaire I just gave an example for a possible use case. Generally you want to avoid defining anything in global

    • @spookyconnolly6072
      @spookyconnolly6072 8 месяцев назад +1

      ​@@yourDecisi0nthat sort of decision decision has hamstringed es6 adding flattening methods to Array because of some library pushing shit to Array

    • @proosee
      @proosee 8 месяцев назад +2

      @@yourDecisi0n Wow, I get when people add something to global objects like window as sometimes there is no better way (e.g. Flutter does it), but modifying primitive types is another level of asking for troubles...

  • @OferGalSabaShelJuba
    @OferGalSabaShelJuba 8 месяцев назад

    Maybe you can help with "CKEditor" component. It complains all the time "Cannot find the "CKEDITOR_VERSION" in the "window" scope."
    Some googling gave me a javascript solution that Typescript did not agree with.
    This video solved the type script issue but it did not stop the error.
    I added
    declare global {
    interface Window {
    CKEDITOR_VERSION: string;
    }
    }
    and in the component onInit() I added
    window.CKEDITOR_VERSION = '5'; //ckeditor5PackageVersion;window.CKEDITOR_VERSION = '5'; //ckeditor5PackageVersion;
    Can you tell what my mistake is?
    Thank you.

  • @CaleMcCollough
    @CaleMcCollough 8 месяцев назад

    If JavaScript could fix it's hacky problems between CommonJS and ModernJS and add a 64-bit integer then TypeScript would be a lovely language. There are some places where you want to pull your hair out with JS. I like the way TypeScript does file level scoping.

    • @recursiv
      @recursiv 8 месяцев назад

      +1. I'd also like the ability to define equality semantics on a per-type basis so that I can use an (x, y) tuple as a Map key, for instance.

  • @RegalWK
    @RegalWK 8 месяцев назад +1

    Great video, I do this for adding types to some company internal libs that are written in pure js