Don't put your types in .d.ts files

Поделиться
HTML-код
  • Опубликовано: 6 июл 2023
  • TypeScript doesn't support this pattern: github.com/microsoft/TypeScri...
    Become a TypeScript Wizard with my free beginners TypeScript Course:
    www.totaltypescript.com/tutor...
    Follow Matt on Twitter
    / mattpocockuk
    Join the Discord:
    mattpocock.com/discord

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

  • @mattpocockuk
    @mattpocockuk  11 месяцев назад +37

    New audio setup arrived today - thanks for bearing with me during my failed Shotgun Mic experiment.

    • @herrpez
      @herrpez 11 месяцев назад

      You pre-empted me and my commenting, you rascal! 😉
      Tbh though, whatever you've got going could work just fine as long as you eliminate the echo. That is to say, sound treating the room.

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +2

      @@herrpez If you're on Twitter, you'll be able to see the latest no-echo setup:
      twitter.com/mattpocockuk/status/1677624336285421569

    • @herrpez
      @herrpez 11 месяцев назад

      @@mattpocockuk Sounds a lot better, and is no doubt a great deal easier than modifying the environment to suit the shotgun mic. 🙂

  • @TomasDohnal10
    @TomasDohnal10 11 месяцев назад +153

    I think folks do it cuz:
    1) The ".d" part of the filename makes it seems like it's where you should define types or something
    2) When you open any library "d.ts" file and you'll see that it's a file full of type definitions, one can get the impression that "d.ts" files are simply files where you define types you wanna use across your project
    (not saying that's right, just describing the impression i had had)

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +32

      Absolutely! I was surprised how many people got that impression.

    • @DisturbedNeo
      @DisturbedNeo 11 месяцев назад +28

      I think the real reason is that it makes the types global with no imports necessary.
      Devs love globals, no matter how bad it makes the code smell.

    • @evol777
      @evol777 11 месяцев назад +9

      That's why I generally don't like 90% of the web devs I've encountered: they assume lots of stuff and usually think others are in the wrong, not them.
      Web devs are the forever wanna be devs, not putting enough work to understand their flaws and wanting others to bend to their flawed necessities.
      And I'm a web dev.

    • @Tommy-nn8xf
      @Tommy-nn8xf 11 месяцев назад +1

      That is exactly me. I put all my types in .d.ts file, maybe I should stop doing that from now on. Should I just put them in a .ts file then?

    • @MAXHASS-ph5ib
      @MAXHASS-ph5ib 11 месяцев назад +5

      @@Tommy-nn8xf you should only use .d.ts files if you're declaring types globally but otherwise you should just use .ts files if you're importing them

  • @twothreeoneoneseventwoonefour5
    @twothreeoneoneseventwoonefour5 11 месяцев назад +77

    I use .d.ts to augment some types and interfaces of the external libraries.
    Example: there is a leaflet map library, and there are a bunch of plugins that are written in pure js, but they add to the library's inbuilt features. So to make typescript happy, I declare those types there.

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +25

      Yep - great idea.

    • @dvillegaspro
      @dvillegaspro 11 месяцев назад +1

      Nice, I hadn't thought of doing this!

  • @code-island
    @code-island 11 месяцев назад +49

    Hi Matt, keep this content up, I really love improving my typescript skills with you

  • @PostMeridianLyf
    @PostMeridianLyf 11 месяцев назад

    Thanks so much for this info Matt! Really helpful!

  • @sun33t
    @sun33t 11 месяцев назад +1

    Excellent guidance Matt, thanks 🎉

  • @artu-hnrq
    @artu-hnrq 11 месяцев назад

    I'm happy to follow these TS discussions here! Thx

  • @shadowplay1211
    @shadowplay1211 11 месяцев назад +47

    I prefer separating types into different files inside a "types" folder that contain types for specific things (like database types in one file, utility types in other file and some app stuff in 3rd file), I don't like the approach of putting every single type into 1 file bc especially in large applications we end up with a huge and hard to maintain types file. Also, I put types into types files only if I have to use them somewhere else, otherwise I would just define a type right in the file that will use that type

    • @HCforLife1
      @HCforLife1 11 месяцев назад +6

      I think all depends on the scope of the project and it's nature. If it's big one with hundreds of PR's and thousands types defined having a giant single file would be ridiculous 😂 in my experience the best is a mix. If you have some types u use Everywhere - use them in global scope. If you have types you use only where you defined it or use them somewhere else where you import it - use them within the file. If you have complex use case where some interfaces are shared, some are used separately - create one file if that benefit the clarity. Also worth to discuss these as you go with your teammates to make sure that you all following the same patterns and that you all agree. But this actually should be a part of Code Review and initial phase of a project

    • @markbutterworth9598
      @markbutterworth9598 11 месяцев назад

      I do the same and for the first time I disagree with Matt on this one. If you put the types in a file co-located with the items using it (sometimes to solve circular references) and don't make it a definition file then you will now have an empty module output.
      I usually put the types in the same file but sometimes they need to be separated out for readability of the code it's typing for the other Devs in the team.
      I do agree there should never be actual code in a .d.ts file but then doing that had never even occurred to me.

    • @gunarcom
      @gunarcom 11 месяцев назад

      Yup, I type stuff coming from APIs in d.ts files because they are global and are passed around through components. Seems like the correct way to do it so I am not having to redefine types every time I use them.

  • @yega3k
    @yega3k 10 месяцев назад +3

    I’ve been using TS because of ReactNative for years but it is only now that I’m realizing how much separate TS knowledge I need. I’m learning so much from your channel. Thank you the breakdowns!

  • @ShotgunAFlyboy
    @ShotgunAFlyboy 11 месяцев назад +5

    I used to write .d.ts files for ambient type suggestions via jsdoc before we took the time to swap to typescript's actual runtimes. It was a big project and it took a while to have the time to do the migration.

  • @SlapThaFunk
    @SlapThaFunk 10 месяцев назад

    This litterly blew my mind. Thank you so much!

  • @CHEBURAT0OR
    @CHEBURAT0OR 5 месяцев назад

    Hello, Matt. Thank you so much for your videos!

  • @abdulachik
    @abdulachik 11 месяцев назад

    awesome dude, i was not sure about this but now its pretty clear

  • @eldbudd
    @eldbudd 11 месяцев назад +1

    A huge smile spread on my face when i read the title of this video.
    About a year and a half ago i started my first frontend dev job, and on my very first task, on the very first week, i stumbled upon our app's huge usage of the .d.ts files. I tried asking several of my peers what is this ".d.ts" file, and nobody knew, one assumed that the "d" stands for "deprecated" (to which i replied in a question - "so why did you declare new types in a new .d.ts file just two weeks ago?").
    So i did a bit of investigation and soon enough i understood the depth of the antipattern that is spread all over.
    You see, i came from Python, and in Python, as Python is, it is extremely easy to make a huge mess and chaotic code, because no typing is required. So i was very sensitive to this issue.
    So at the end of this first week, and actually up until today, because this thing is like cancer, my crusade is to get rid of all our .d.ts files.
    Good luck to me.

  • @kengreeff
    @kengreeff 11 месяцев назад

    Great info! Thank you

  • @foreach1
    @foreach1 11 месяцев назад

    The youtube algorithm did a great job showing me this channel 🙌 subscribed

  • @ikechukwucharles2314
    @ikechukwucharles2314 11 месяцев назад

    Well said Matt. When writing typescript code is usually have a directory "types" in which files that add my own types to the global namespace using declare global ...
    Good one 👍

  • @Zagoorland
    @Zagoorland 11 месяцев назад +5

    I thought that d.ts was just naming convention for type related files and a lot of my dev friends also, lol…

  • @Smileyassassin47c
    @Smileyassassin47c 11 месяцев назад

    Hehe, i actually was wondering about the best practices on this one and found your blog post. Nice to see the video too.

  • @MrBrax
    @MrBrax 11 месяцев назад

    Thanks, will change my project structures now and credit this!

  • @AbdallaElmedny
    @AbdallaElmedny 11 месяцев назад

    Super useful! I created a monorepo with a frontend and backend, and decided to create a third project for shared types. In beginning using .d.ts seemed like logical approach. But it gave me weird problems, and was indeed not being type checked. This is because of skiplibcheck! Im going to stop using them and switch to .d.ts. Thanks so much!!

  • @dawid_dahl
    @dawid_dahl 11 месяцев назад

    Great video, thank you!

  • @rajd2990
    @rajd2990 3 месяца назад

    Thanks for such a great tutorial 👍👍

  • @adamhorvath6649
    @adamhorvath6649 11 месяцев назад

    Thanks Matt!

  • @GabrielPozo
    @GabrielPozo 11 месяцев назад

    Thanks for share this 👏🏾

  • @alpakarosa3678
    @alpakarosa3678 11 месяцев назад

    I liked your new scenario 😮

  • @dvillegaspro
    @dvillegaspro 11 месяцев назад +2

    Thanks for the revelation, Matt. I think I do this because types feel like such a specific feature of a codebase to me that it doesn't make sense for type declarations to be modular inherently. It made more sense in my head for them to be global; if TS doesn't end up in the bundle anyway, why should I care about the import statements? That's just how I thought about it, but never thought that it could be an issue.

  • @Gabriel-iq6ug
    @Gabriel-iq6ug 11 месяцев назад

    At least 40% of the devs following you have still a lot to learn, including me. Good for you!

  • @chris94kennedy
    @chris94kennedy 11 месяцев назад +1

    Thanks Matt as per usual. The Typescript Don.

  • @matttamal8332
    @matttamal8332 11 месяцев назад +1

    Hello, you're awesome. Great content dude!

  • @gosnooky
    @gosnooky 11 месяцев назад +3

    All my projects have an index.d.ts in the project root where I augment libraries, like adding properties to the express req object, for typing process.env and adding semantic alises like type IsoDate = string or type UnixTimestamp = number.

    • @KurtLippert
      @KurtLippert 11 месяцев назад

      I do this as well. I thought that was the use case for them. Is there an alternative?

  • @weirdwordcombo
    @weirdwordcombo 11 месяцев назад +1

    I sometimes use global types for automatic declaration merging because I need to extend types that I share with my git submodule(s). This means i can share types with my submodule, but also extend these types with project specific stuff. I don't use .d.ts files for this, instead I use "declare global" in normal module files. Before watching your vid, I would have said it doesn't make a difference, but with your skipLibCheck advice, it does actually.

  • @Remeroska
    @Remeroska 11 месяцев назад +8

    I have an answer to your question: Why do people use them?
    I can answer anecdotally with my experience: because ts docs are a mess, and you didn't yet made an explaination video about it.
    2 takeaways: - ts docs should be rewritten from scratch - you are a rare gem, thanks for existing ❤

  • @ThomasBurleson
    @ThomasBurleson 11 месяцев назад

    This is one of your MOST useful videos.
    I have suffered the pain of this from so many teams.
    I stumbled trying to clearly explain WHY *.d.ts files are bad bad bad.
    You nailed it with this video : so clear, so very useful.

  • @hicoop
    @hicoop 11 месяцев назад

    Awesome video like usual! I have a d.ts for altering nextjs’s special @auth/core session

  • @MohitKhare
    @MohitKhare 11 месяцев назад

    thanks Matt.

  • @marceloprado2035
    @marceloprado2035 11 месяцев назад

    great content!

  • @Dev-Siri
    @Dev-Siri 11 месяцев назад

    previously I had a terrible typing. (literally a props.d.ts file that was an interface-export for all the props in a React project)
    Now I learned how to learn and realised types don't need to be
    seperated
    or in .d.ts

  • @hypergraphic
    @hypergraphic 11 месяцев назад +1

    That's very helpful to know! Something else I've wondered, is should we put centralize the types, or have them distributed around the codebase. At work, we have a types folder with multiple files for each kind of category like auth.ts, billing.ts, etc. Is that an antipattern?

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +2

      Here you go!
      www.totaltypescript.com/where-to-put-your-types-in-application-code

    • @hypergraphic
      @hypergraphic 11 месяцев назад +1

      @@mattpocockuk thanks!

  • @jcollins519
    @jcollins519 11 месяцев назад

    Thanks for confirming the I've been doing it right this whole time 😂

  • @scalenescales
    @scalenescales 11 месяцев назад +1

    I use .d.ts files for defining types in the global scope. I used to use it a lot less, but because these files only really want to reference other globally defined types, it ends up being parasitic. So, I've gradually moved all of my type definitions to .d.ts files.
    This actually ends up being really convenient and save a lot of refactoring effort because when you move a folder or file around, you don't have to update the corresponding type file as well. VS Code seems to be able to handle moving a single file around but not when you move multiple interdependent files / folders. IIRC, I first started doing this because it turned a ~100 line refactor that I was having trouble double-checking to ~3 lines. Note, I also avoid specifying type annotations because TypeScript can usually although not always correctly infer the types because it saves work whenever I change the parameter or return type.
    I could make refactors easier by having a global location for types, but this is inconvenient because it makes navigating between type definitions and implementations more difficult (e.g. ctrl+click on a type and navigate to the adjacent file vs hover over parameter, wait for intelli-sense, copy type, search for the implementation among dozes of results) because they are no longer co-located.
    However, the project I'm currently working on is a game with a lot of complicated interactions, so everything has to depend on everything else. I don't think this would be nearly as relevant / beneficial for a normal web app.

    • @theseangle
      @theseangle 5 месяцев назад

      And that's why tsconfig.json has the "paths" property... You make the paths relative to the workspace folder, and voila, no 100 lines "refactors" when you move files around.
      Plus if VSCode's automatic path modification doesn't reliably work there's something really wrong with your project's setup

    • @scalenescales
      @scalenescales 4 месяца назад

      I was already using paths to setup absolute file paths. It's one of the reasons why I'm using absolute files paths because it solves the issue for some of the scenarios but not all of them. Those refactors happened in spite of that.
      Maybe you can replicate the one of the issues I was dealing with?
      1. Create a new vite ts react app.
      2. Create a folder (a) with multiple files each exporting something.
      3. Create a folder (b) with multiple files each importing some of those exports.
      4. Without having any of the importing files opened, move the folder (b) to another sub-folder.
      On my setup, this results in the imports the files in folder (b) not being updated. It works if the files in folder (b) are opened, but that's not practical for obvious reasons.
      I believe absolute imports would solve this particular issue at the cost of introducing a different issue, but it's just one of many different issues I encounter when moving multiple files/folders around in VS Code (e.g. github.com/microsoft/vscode/issues/105110). Again, moving a single file works just fine, it's various flavors of moving multiple files and folders at once that causes issues.

  • @andreyokhrimenko2271
    @andreyokhrimenko2271 11 месяцев назад +1

    Thanks for the information, but I haven't seen the use of .d.ts files in any tutorial project other than declaring types and interfaces. However, if you create a types.d.ts file in the root of the project, you can use types and interfaces without importing them, but just by specifying types (interfaces), which in my opinion is very convenient.

  • @william3588
    @william3588 11 месяцев назад

    What would be the correct approach when building a component library with typescript? ts, tsx files? I've seen d.ts files referenced on the types property of the package.json file

  • @emreq9124
    @emreq9124 11 месяцев назад

    Your keyboard sound is awesomr. What is its brand and model?

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

    I was first confused by the title and then realized that it is about people creating .d.ts files manually. I never thought anyone would work with .d.ts files except for compiling into a package or importing typings :D

  • @gabrielchaves2282
    @gabrielchaves2282 11 месяцев назад

    Looking fresh

  • @NutchapolSal
    @NutchapolSal 11 месяцев назад +1

    i think a better explanation is that it is for compiled ts code that has become js, not for us to write directly
    so you can have the actual runnable code, and the types for developers to use

    • @yega3k
      @yega3k 10 месяцев назад

      This is what I got from the included link.

  • @hamodeyDooba
    @hamodeyDooba 11 месяцев назад +2

    i have to like before watching 🎉
    enough for me to see you publishing new content

  • @AlexanderCerutti
    @AlexanderCerutti 11 месяцев назад +2

    Hi Matt, thanks for this! Very interesting. About skipLibCheck, I thought it was about all the typings, not just custom ones. But why disabling it? I mean, wouldn't it be better to have it disabled ("false") so TS warn you are making code-smell?
    Would it be perhaps be better to have a tsconfig.build.json file that turns it true while on development it is turned to false?
    I'm used to always create two tsconfig files, one for build and one for development. What's your suggestion here? Do you agree with this way of making ts projects? Thanks!

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      You definitely want the same rules in your tsconfig in dev and production. Much easier maintenance and keeps things more secure in the long run.

    • @AlexanderCerutti
      @AlexanderCerutti 11 месяцев назад +1

      @@mattpocockuk I make tsconfig.json to extend tsconfig.build.json, so what's really important stands in production file while things like "paths" stays in the dev

  • @naraksama3903
    @naraksama3903 11 месяцев назад +3

    Putting my internal types into a global.d.ts file caused me a lot of headaches to actually get it to work and then realize that this approach was completely stupid. Not only because Typescript interprets two different types of declaration files (one with imports and one without), but also because it's very cumbersome since you have to re-type all the used internal classes because of the type-value difference.
    I learned that you should not use declaration files if the compiler does not complain about missing types from external modules or unknown imported file types it cannot read.

  • @ElendiarDev
    @ElendiarDev 3 месяца назад

    Thanks, that exactly that info i want.
    I wondered why I shouldn’t store all my types and interfaces in d.ts, because it looked convenient - I don’t have to import them.
    But I suspected there was a catch somewhere...

  • @waqaskhandev
    @waqaskhandev 11 месяцев назад

    I was using it the right as you explained but any way it was good to know that I can make that mistake.

  • @shivan2418
    @shivan2418 11 месяцев назад

    What keyboard do you use? It sounds really nice.

  • @kahdeksan
    @kahdeksan 11 месяцев назад

    i always have a .d.ts file for my glsl files:
    declare module "*.glsl" {
    const value: string
    export default value
    }
    is this the correct way to do this? or should i use plain .ts?

  • @ragtop63
    @ragtop63 11 месяцев назад +1

    I mean, we are talking about a superset of features (because Typescript really isn’t a language) that was designed to make a scripting language do something it wasn’t designed to do. And the bandage keeps getting bigger and bigger.

  • @learner8084
    @learner8084 11 месяцев назад

    Thanks. I've not known what's the .d.ts for previously. Now I know.

  • @QubitGlobe
    @QubitGlobe 11 месяцев назад +1

    clear explanation. I like your videos. keep producing Matt.

  • @thekwoka4707
    @thekwoka4707 10 месяцев назад

    Can you do a video on why packages that ship with types, should include the declaration maps and source ts files in the package? It's very annoying not having `go to definition` work.

  • @frankjarvis3531
    @frankjarvis3531 11 месяцев назад

    can you talk more about module augmentation?

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

      What do you want to know?

  • @soviut303
    @soviut303 11 месяцев назад +22

    I think a video demonstrating when you would transition a type from a normal module to a d.ts file would be very helpful. Some kind of simple project where a User type needs to be changed from local to global; something like that.

    • @PhatOof
      @PhatOof 11 месяцев назад +2

      If it's your code you probably shouldn't be doing it. You should be using it to type /alter types from imported modules

    • @johnnyhane6337
      @johnnyhane6337 11 месяцев назад +1

      I think one of the points he's making in the vid is that you shouldn't use .d.ts files for your own types. They're really only used for adding types to js files (like in the case of npm packages) or weird hacky type overrides

    • @soviut303
      @soviut303 11 месяцев назад

      @@johnnyhane6337 That's not the only case. In that situation you'd be using define module.

    • @soviut303
      @soviut303 11 месяцев назад +1

      @@PhatOof That isn't the case at all. There are plenty of times when you want global types in a project.

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

    Hello Matt, thank you for all these useful videos about TS. I'm currently binge-watching them.
    I use spatie/typescript-transformer to convert my PHP code to TypeScript (in a Laravel-Vue-Inertia stack). Let's say I have a DTO UserData with name and email as properties, this class then gets transformed into a App.Data.UserData type that's available globally without importing via .d.ts file.
    For me it's very convenient since I don't need to import these DTO everytime I need them and they are generated automatically (from PHP code).
    Is that a bad design as well? To me it ressembles all these ".d.ts" that accompany ".js" files like explained in the GitHub post you included in the description box.

  • @pixiedev
    @pixiedev 11 месяцев назад

    thanks to save my time buddy 👍🏻.

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

    I only use d.ts files to declare env variables or global things

  • @user-jd7ub3tq2b
    @user-jd7ub3tq2b 11 месяцев назад +1

    I really like your videos about the features of typescript. I would like to look at the full systematized cycle about typescript from you

  • @Netherlands031
    @Netherlands031 11 месяцев назад +1

    So when should you use it - specifically when you have a .js with types?

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +3

      1. When you need to type some JavaScript
      2. When you want to make alterations to a global type, like in this article: www.totaltypescript.com/how-to-properly-type-window

  • @iamworstgamer
    @iamworstgamer 9 месяцев назад

    what do you suggest we do for javascript legacy code?

  • @aaronmcadam
    @aaronmcadam 11 месяцев назад

    What about when you want ambient types? Like when you need to extend or define a type for a library?

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      Absolutely fine - global changes are what d.ts files are for!

  • @collinslagat3458
    @collinslagat3458 11 месяцев назад

    Damn. You live you learn.

  • @eof_lemongrab
    @eof_lemongrab 11 месяцев назад

    That keyboard sounds delightfully thocky. Which brand/model is that?

  • @CarlesRojas
    @CarlesRojas 11 месяцев назад

    What keyboard is that? The sound is heavenly!

  • @barinbritva
    @barinbritva 11 месяцев назад

    What's about things like: declare global { interface Window { /*extends*/ }}? Should it be .d.ts? It's a global thing, not a module? Also, sometimes it's necessary to radicle some module typings: declare module 'universal-router' { /* some magic here */ }. What would you recommend doing in those cases? Thank in advance

  • @ndukachukz8067
    @ndukachukz8067 11 месяцев назад

    I only use them for my types and interfaces never had a module there

  • @typeguard
    @typeguard 11 месяцев назад +1

    Found this out the hard way a while ago. Now I only use .d.ts files to declare certain modules I need to have globally available. For example, if I'm using SCSS modules, Typescript normally doesn't know or understand what they are so I declare them in my types.d.ts as
    declare module "*.module.scss" {
    const content: { [className: string]: string };
    export default content;
    }
    I suppose this is what it was meant for

  • @nephis_2006
    @nephis_2006 11 месяцев назад

    Ty, my coworkers will listen to me now

  • @SakuraDev
    @SakuraDev 11 месяцев назад +4

    Hi, I didn't know that .d.ts files push the types in the global scope. Really love that feature. Love the video as well 🙏

    • @rubendacostaesilva8442
      @rubendacostaesilva8442 11 месяцев назад

      It's a cool feature, but you will have to be careful naming your types, specially if you're using React, or any other library or framework, because you could be naming a type with a name that already being used by that library/framework, and if you're not paying attention, you could end up choosing the wrong type.

  • @OmkarBhoir07
    @OmkarBhoir07 11 месяцев назад

    How to handle the npm lib which doesn't support TS. That's the reason i disabled the lib type check in config.

  • @KevinOsterkilde
    @KevinOsterkilde 11 месяцев назад

    Love your new recoding setup but it's quite echo-y?
    Anyway, appreciate the content as always ❤

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +2

      Agree, I tried a shotgun mic for a couple of vids but it's not working, new mic coming tomorrow.

    • @KevinOsterkilde
      @KevinOsterkilde 11 месяцев назад

      @@mattpocockuk Looking forward to it, always happy whenever you drop new knowledge for us wizards ⚡

    • @staticyawn
      @staticyawn 11 месяцев назад

      @@mattpocockuk maybe you wanna consider putting foam on the walls so the sound doesn’t bounce as much

  • @samuelgunter
    @samuelgunter 11 месяцев назад +1

    so if you wanted to make a package that was just a type definition to publish on npm, for example an AnyStringWithAutocomplete (that weird "| (string & {})" or something like that, you just have a .ts file instead of a .d.ts file?

  • @LivvieLynn
    @LivvieLynn 11 месяцев назад

    Can you declare global types outside of the types.d.ts file? I know you can export and import individual .d.ts files throughout a project but I'd like to better organize global types.

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +2

      declare global {
      interface MyGlobal {}
      }

  • @elab4d140
    @elab4d140 11 месяцев назад

    thanks

  • @TehHayzen
    @TehHayzen 11 месяцев назад

    I remember reading that .d.ts files were skipped during complication which can make a significant difference in large codebases. When you create a .ts module strictly containing types, what does the compiler do? Does it just produce an empty .js file?

  • @Raul-si7bn
    @Raul-si7bn 10 месяцев назад

    I have a related question that maybe somebody have faced before :
    If i create a `customTypes.ts` file, where I create and export some interfaces/types, and i put that file inside the folder where I have my `main.ts` file (where I write my code), How could I exclude that file/folder from the compilation?
    I'd like that the compiler do not create a `customTypes.js` when compiling.
    I tried to create a ` "exclude":["customTypes.ts"] ` in my `tsconfig.json`. , but since the file contains "export" declarations , the compiler walks through that file anyway.
    Do you have any standar solution, or a way to really exclude a file with import/export statements ?

  • @pxkqd
    @pxkqd 11 месяцев назад

    I just use them for external libs or dependencies that don't have typescript, or adding things to the window type.

  • @antonarbus
    @antonarbus 11 месяцев назад

    I’m running to fix my code before my tech manager watched this video

  • @echobucket
    @echobucket 11 месяцев назад

    I also think maybe some people think of these like .h files in C. It's a place to put the type definitions and function definitions....

  • @user-su3dq3xe8i
    @user-su3dq3xe8i 9 месяцев назад

    I use .d.ts to declare some project specific types that I want to use everywhere in the project. That way they got into global scope so I don't need to import them in every single module that uses them. I also use namespaces, so different parts don't clash with each other. What's wrong with that?...

  • @hyperprotagonist
    @hyperprotagonist 11 месяцев назад

    Such a pet peeve. I see it in Discords ALL the time... "just put it in a declaration file because I can't be bothered to educate you".

  • @butcher
    @butcher 10 месяцев назад

    If we have a shared protocol file with interfaces, is it a good idea to use d.ts ? Or is it only for types?

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

      What do you mean by a 'shared protocol file'?

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

      And by types, I'm referring to types declared using the 'type' keyword or the 'interface' keyword.

  • @w01dnick
    @w01dnick 11 месяцев назад

    Hi, I have somewhat related question. To describe external to TS data (which came from JS) I use `declare` in .d.ts. But I can't use types from modules in there. So I have to put all types in .d.ts too. Is there any way to avoid putting all those types in global scope?

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      You can use declare global in a .ts file, might be easier if most of your types are in modules anyway.

    • @w01dnick
      @w01dnick 11 месяцев назад

      @@mattpocockuk Thank you. Am I understand it correctly, that I'll have to put that declare global in each .ts file using those data directly?

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      @@w01dnick I'd need more info to answer you properly. Can you ask in mattpocock.com/discord ?

  • @tcortega
    @tcortega 11 месяцев назад

    i like your channel

  • @utkutekalmaz
    @utkutekalmaz 11 месяцев назад

    I just have one .d.ts file which is to create a custom type safe axios client like this:
    import 'axios';
    declare module 'axios' {
    export interface AxiosRequestConfig {
    retry?: number;
    retryDelay?: number;
    }
    }
    am I doing wrong? :(

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      Nope, that's a global alteration - not a file containing types!

  • @farahrayis5928
    @farahrayis5928 11 месяцев назад

    I think I have read about this in the docs...

  • @DS-rq3gq
    @DS-rq3gq 11 месяцев назад +1

    I used to do exactly this until I found out you can't put enums in .d.ts files. That's when I learned I was doing it wrong

  • @georgethenewbie
    @georgethenewbie 4 месяца назад

    Hi @mattpocockuk - I watched the video more than once and I'm confused between the title of it and its content. Can I use an env.d.ts global file in my Astro app for example to declare a type that I know for a fact I'll use throughout my codebase? Is this not recommended?

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

      Globals are just a bit iffy - but I suppose a global type is basically fine.

  • @SamInNZ
    @SamInNZ 11 месяцев назад

    Would you advise against using d.ts to store global type helpers?

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

      No - globals is what .d.ts files are for!

  • @kirillzlobin7135
    @kirillzlobin7135 11 месяцев назад

    So the files should be named the same, but with different extantions. In this case we will not need to write explicit import?

  • @workflowinmind
    @workflowinmind 11 месяцев назад

    Matt, I'm not kidding please write a ts book, I'll buy it instantly

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

      Might be something in the works here...

  • @ward7576
    @ward7576 11 месяцев назад

    This explanation pretty much put you on main part of normal distribution where on the left side there's a noob and other side senior, saying "it works, don't care".

  • @aminmahjoob2874
    @aminmahjoob2874 11 месяцев назад

    nice

  • @snithfferx
    @snithfferx 11 месяцев назад

    So.
    Do you recomend to use the d.ts files as typescript version of .env files?
    I mean, you don't want to ship your aplication with your configuration, right.

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

      What do you mean?

    • @snithfferx
      @snithfferx 11 месяцев назад

      @@mattpocockuk I have recently have began with node.js, and I was follow a tutorial where logically they let his connection file at the main root folder (src) with all database connection information, so a google if there is a way to use as other languages the .env file or sort of file to populate with config info and import it into the typescript application, but there is a one way, the global.d.ts file, I don't really if I can change the name, but in this you can define the .env variables to use at typeorm

    • @mattpocockuk
      @mattpocockuk  11 месяцев назад +1

      @@snithfferx .d.ts files aren't used at runtime. They can only be used to provide global types, not global runtime values.

    • @snithfferx
      @snithfferx 11 месяцев назад

      @@mattpocockuk OK. thanks. To keep loking for a way to do it then.