Тёмный

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

Matt Pocock
Подписаться 105 тыс.
Просмотров 137 тыс.
50% 1

TypeScript doesn't support this pattern: github.com/mic...
Become a TypeScript Wizard with my free beginners TypeScript Course:
www.totaltypes...
Follow Matt on Twitter
/ mattpocockuk
Join the Discord:
mattpocock.com...

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 291   
@mattpocockuk
@mattpocockuk Год назад
New audio setup arrived today - thanks for bearing with me during my failed Shotgun Mic experiment.
@herrpez
@herrpez Год назад
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 Год назад
@@herrpez If you're on Twitter, you'll be able to see the latest no-echo setup: twitter.com/mattpocockuk/status/1677624336285421569
@herrpez
@herrpez Год назад
@@mattpocockuk Sounds a lot better, and is no doubt a great deal easier than modifying the environment to suit the shotgun mic. 🙂
@TomasDohnal10
@TomasDohnal10 Год назад
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 Год назад
Absolutely! I was surprised how many people got that impression.
@DisturbedNeo
@DisturbedNeo Год назад
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 Год назад
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 Год назад
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?
@Tommy-nn8xf
@Tommy-nn8xf Год назад
@@MAXHASS-ph5ib Thanks for the suggestion!
@ward7576
@ward7576 Год назад
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".
@GregRippetoe
@GregRippetoe 7 месяцев назад
So there's no real reason to not put your types in .d.ts files apart from personal preference
@shadowplay1211
@shadowplay1211 Год назад
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 Год назад
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 Год назад
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 Год назад
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.
@BjornBrasse
@BjornBrasse Год назад
At 1:20 Let me show you exactly why they're so bad... But a straight up answer never comes (only indirectly)... So I think I understand, but the explanatation doesn't go into why declaring types globally is a bad idea. And why can packages use them for sure, but you shouldn't as module declaration files... Still unclear to me. 😊
@code-island
@code-island Год назад
Hi Matt, keep this content up, I really love improving my typescript skills with you
@twothreeoneoneseventwoonefour5
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 Год назад
Yep - great idea.
@dvillegaspro
@dvillegaspro Год назад
Nice, I hadn't thought of doing this!
@Zagoorland
@Zagoorland Год назад
I thought that d.ts was just naming convention for type related files and a lot of my dev friends also, lol…
@guilhermeferreirabr
@guilhermeferreirabr Год назад
So basically roughly half of the devs do not know what (or why) they are doing 😂Proper training and standards should be mandatory in our field ...
@Remeroska
@Remeroska Год назад
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 ❤
@ricardorng
@ricardorng Год назад
this happends because programmers don't know bases...
@samuelgunter
@samuelgunter Год назад
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?
@mattpocockuk
@mattpocockuk Год назад
Yes
@ShotgunAFlyboy
@ShotgunAFlyboy Год назад
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.
@nwsome
@nwsome Год назад
In my experience, in 90% of cases types.ts is also a bad idea. Just define types, where you use them.
@mattpocockuk
@mattpocockuk Год назад
Yes, most types should be colocated where they're used. But if you have shared types, then a types.ts file is fine.
@jeromealtariba7339
@jeromealtariba7339 Год назад
agreed
@neenjawtim
@neenjawtim Год назад
Is there an effort to upstream this information in to the ts handbook? I think if this is so common, this should be spelled out more plainly there.
@DisturbedNeo
@DisturbedNeo Год назад
I think it’s fine to have a “types” or “models” folder, as long as it’s one type per file. If I have a User model, that’s gonna be used in many places, so it should in a central, shared location. Otherwise you’re either going to be redeclaring it in various files, or potentially importing the “User” type from some unrelated component.
@ihateorangecat
@ihateorangecat Год назад
it's only true for a todo app. lol😆
@naraksama3903
@naraksama3903 Год назад
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.
@hamodeyDooba
@hamodeyDooba Год назад
i have to like before watching 🎉 enough for me to see you publishing new content
@gosnooky
@gosnooky Год назад
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 Год назад
I do this as well. I thought that was the use case for them. Is there an alternative?
@DS-rq3gq
@DS-rq3gq Год назад
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
@yega3k
@yega3k Год назад
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!
@ragtop63
@ragtop63 Год назад
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.
@eldbudd
@eldbudd Год назад
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.
@typeguard
@typeguard Год назад
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
@mattpocockuk
@mattpocockuk Год назад
Absolutely!
@AnthonyPaulT
@AnthonyPaulT Год назад
after this video, i still don't understand what it's for
@PieterWigboldus
@PieterWigboldus 7 месяцев назад
I think you have to reevaluate your sentence. That you dont do it when you write Typescript. But for JavaScript, you can do it. If you generate JavaScript from your Typescript code, or just writing JavaScript itself. you have a prejudice that everyone writes Typescript. Maybe it is your preference, but not for everybody. At the end, if you write Typescript or JavaScript, your production code is JavaScript (your application and npm package), with our without d.ts files. I think you only prefer to write Typescript, and dont want to write the d.ts itself by hand.
@miervaldis42
@miervaldis42 5 месяцев назад
I agree with you but then, I was wondering why npm suggests you to create a `.d.ts` file when it could have just told you to have types files ? N.B. : I am currently creating a React npm package and I just saw the npm message...
@riendlyf
@riendlyf 2 месяца назад
PLEASE someone, can you create a video on HOW TO USE script CORRECTLY ?
@JordanICM
@JordanICM 4 месяца назад
I still dont understand the issue. I use d.ts files to make my types global so I don't have to import them into every file. What if every time I wanted to use "let" or "const", I had to import it? That would get annoying. So for certain other common fundamental types in my app, I put them in a d.ts file. Is that blasphemy?
@ElendiarDev
@ElendiarDev 6 месяцев назад
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...
@hrax
@hrax Месяц назад
Just found this video as I am learning TS. Well, time to refactor my code, while it is still fresh. :D
@martonvalentin7386
@martonvalentin7386 Год назад
But if you put your types without "export" keyword into .d.ts files, tsc fails compiling. (Cannot find name "...") Am I missing something ?
@dvillegaspro
@dvillegaspro Год назад
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.
@Alexander-by4uv
@Alexander-by4uv Год назад
Bad technology where people have to struggle with the question where their types go to ;-) Don't understand me wrong, TypeScript is the best thing which could happen to JS. But JS itself is horror and deserves to be forbidden :P
@AlexanderCerutti
@AlexanderCerutti Год назад
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 Год назад
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 Год назад
@@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
@doc8527
@doc8527 Год назад
After seeing tons of garbage typescript definition file all over the place from different projects time to time, where you have to constantly jump between different files to check out and correct the typescript. Similar to when you have a big CSS file or multiple CSS files where you constantly have to hop over to check out a specific class style. For 99% of the times and non library development, if there is no hard guideline for the typescript definition across the team, I simply put typescript definition on the top of the same file that is using the typescript if it's not going to be shared anywhere. No import, no export, less typescript confliction during the review, people change it on demand, scope well, move out of scope on demand, no premature "typescript optimization”, works the best. Big file issue? It's always case by case. Big file can be simple if the code flow is clean, small file can be complex if there are tons of unnecessary nested abstractions everywhere. People will thank me later if they don't need to jump between files to figure out a simple logic.
@jonhobbssmith
@jonhobbssmith Год назад
I'm confused here. At one point you say "that's what declaration files are for, putting things in the global scope". Thrn uoubgononnto say "never put types in the global scope". So, why? And what are you supposed to put in declaration files ifnit's.notnthe types you eant to share? If you have types that are used throuout your projectnthen why eould you want to be importing them in every file? And why are you trying to put runtime code in your type declaration files anyway?
@heinzerbrew
@heinzerbrew Год назад
How about a video on why 40% of the people do this wrong. Is it bad documentation? Are the profession books teaching this wrong? Did everyone just learn it from some random youtuber?
@Hemigoblin
@Hemigoblin Год назад
TypeScript is the last gasp of OOP for real-world programming. You can skew OOP or FP, ya gotta pick one.
@thinkingdev102
@thinkingdev102 Год назад
I use it for declaring types for our private js packages published by our organisation where they didn't bother to use typescript. 🤬
@OmkarBhoir07
@OmkarBhoir07 Год назад
How to handle the npm lib which doesn't support TS. That's the reason i disabled the lib type check in config.
@AntonPenqe
@AntonPenqe Год назад
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?...
@peachesfruitella
@peachesfruitella Год назад
whats interesting is why people use .d.ts in the first place .. as a FE dev we always just used .ts for interfaces ... but the internet kind of suggested otherwise. If it seemed like convention - people will follow. But why did we follow this guide to begin with?
@ExZeMIP
@ExZeMIP Год назад
I've never seen anyone using a .d.ts file using for types used in the actual project, just JavaScript libraries
@noelsoong777
@noelsoong777 6 месяцев назад
I hate how fastify forces me to make d.ts files and use declaration merging for their interfaces. I spend weeks to setup the tsconfig so my monorepo packages work when being used as a dependency.
@ThePizzabrothersGaming
@ThePizzabrothersGaming Год назад
I hate .d.ts files because any time you jump to definition it actually jumps to the types. great, i know the types of this function that i have no idea what kt does..
@NoelSoong
@NoelSoong 6 месяцев назад
I hate how fastify is forcing me to use d.ts files when configuring for it in a more complex file structure like a monorepo is hell.
@echobucket
@echobucket Год назад
I feel like this is the TypeScript docs's fault. I remember trying to read the docs about .d.ts files and not understanding the use cases for them. The docs do not make this clear.
@learner8084
@learner8084 Год назад
Thanks. I've not known what's the .d.ts for previously. Now I know.
@alexmachin1785
@alexmachin1785 Год назад
When i started at my new job i saw this .d.ts file nonsense and well continued the pattern, now i regret my decision 😅 volate your damn types or wirst case have a types folecto share types easily.
@andreyokhrimenko2271
@andreyokhrimenko2271 Год назад
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.
@GT-tj1qg
@GT-tj1qg Год назад
Why would anyone put all their variable declarations in a separate file anyway? What a stupid thing.
@antonarbus
@antonarbus Год назад
I’m running to fix my code before my tech manager watched this video
@duetwithme766
@duetwithme766 Год назад
File based organization of code is terrible period. Time to get rid of it and replace it with something more meaningful
@AngusMcIntyre
@AngusMcIntyre Год назад
Classic case of devs thinking they are smart. Keep it simple, folks.
@mateuszgajo9731
@mateuszgajo9731 6 месяцев назад
Another problem you can run into, is that you can import non existing types to d.ts.
@modernkennnern
@modernkennnern Год назад
I've seen them of course, but I never understood what they did, so I never used them. Yay me, I guess
@matttamal8332
@matttamal8332 Год назад
Hello, you're awesome. Great content dude!
@inthenameoflove6831
@inthenameoflove6831 Год назад
2:40 I think that's wrong. I think that applies to any TypeScript file (ts or d.ts) that is included in a tscofnig (via include array or files array) as long as it doesn't contain any import or export statements. If you use any import or export statement in a d.ts file, types won't be availible globally anymore. At least that how it works for me, don't ask me why. Global types can be useful at times though (for example for some custom helpers like 'UnionToIntersection' or 'DeepPartial').
@chris94kennedy
@chris94kennedy Год назад
Thanks Matt as per usual. The Typescript Don.
@ya4dang1
@ya4dang1 Год назад
So define types in .d.ts files for the sole purpose of not to manually import them else where is a bad idea?
@victorlongon
@victorlongon Год назад
I only use d.ts files to declare env variables or global things
@SakuraDev
@SakuraDev Год назад
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 Год назад
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.
@hsul-git
@hsul-git Год назад
And why not keep types in the component itself when they are not reuseable?
@jonobrien8848
@jonobrien8848 Год назад
in other news, opinions make coding hard. News at 11.
@Netherlands031
@Netherlands031 Год назад
So when should you use it - specifically when you have a .js with types?
@mattpocockuk
@mattpocockuk Год назад
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
@ndukachukz8067
@ndukachukz8067 Год назад
I only use them for my types and interfaces never had a module there
@Linuxdirk
@Linuxdirk Год назад
My types go right before the variable, where they belong.
@michaelharrington5860
@michaelharrington5860 Год назад
I wish I had 50% of your knowledge
@mhadi-dev
@mhadi-dev Год назад
This is what a successful clickbait really means
@zuzukouzina-original
@zuzukouzina-original 9 месяцев назад
So we need to use .d.ts like .h files for c/c++ code? 😳
@Sagarclips
@Sagarclips Месяц назад
clear, consise, crisp - You have awesome voice Matt!
@LetalisLatrodectus
@LetalisLatrodectus 2 месяца назад
If your large code base has js files that are typed with .d.ts files then surely you want skipLibCheck to be false correct?
@mattpocockuk
@mattpocockuk 2 месяца назад
Yes, sadly
@NutchapolSal
@NutchapolSal Год назад
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 Год назад
This is what I got from the included link.
@georgethenewbie
@georgethenewbie 8 месяцев назад
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 8 месяцев назад
Globals are just a bit iffy - but I suppose a global type is basically fine.
@soviut303
@soviut303 Год назад
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 Год назад
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 Год назад
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 Год назад
@@johnnyhane6337 That's not the only case. In that situation you'd be using define module.
@soviut303
@soviut303 Год назад
@@PhatOof That isn't the case at all. There are plenty of times when you want global types in a project.
@pixiedev
@pixiedev Год назад
thanks to save my time buddy 👍🏻.
@farahrayis5928
@farahrayis5928 Год назад
I think I have read about this in the docs...
@boomknuffelaar
@boomknuffelaar 8 месяцев назад
I hate how the .d.ts files are often used in Express apps. The request object is given to middleware and the type of its data property is often defined using a .d.ts file. But all of the properties have to marked as possibly null since that's how the request object starts. And then every single middleware function has to type-check that property again for type safety.
@mattpocockuk
@mattpocockuk 8 месяцев назад
Yeah it's not ideal. trpc does a much better job here.
@hypergraphic
@hypergraphic Год назад
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 Год назад
Here you go! www.totaltypescript.com/where-to-put-your-types-in-application-code
@hypergraphic
@hypergraphic Год назад
@@mattpocockuk thanks!
@lacherolachero9409
@lacherolachero9409 Год назад
that's not completely true, if you export something from a .d.ts file it behaves like a module and not make your types globally accessible
@mattpocockuk
@mattpocockuk Год назад
Correct!
@weirdwordcombo
@weirdwordcombo Год назад
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.
@arcanernz
@arcanernz 10 месяцев назад
I put mine in a .t.ts file for just types
@nephis_2006
@nephis_2006 Год назад
Ty, my coworkers will listen to me now
@i007c
@i007c Год назад
well this is just a bad language design
@kahdeksan
@kahdeksan Год назад
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?
@danieltkach2330
@danieltkach2330 Год назад
C# is an actual typed language 🙂
@Raul-si7bn
@Raul-si7bn Год назад
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 ?
@geraldaburto7712
@geraldaburto7712 7 месяцев назад
What is wrong is to use the `export` key inside the types.d.ts file, right?
@mattpocockuk
@mattpocockuk 7 месяцев назад
Correct!
@geraldaburto7712
@geraldaburto7712 7 месяцев назад
I got confused, It is OK to put my global types inside the types.d.ts file or not?
@mattpocockuk
@mattpocockuk 7 месяцев назад
That pattern is sort of fine, but I'd prefer my types imported and exported.
@barinbritva
@barinbritva Год назад
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
@Cahnisama
@Cahnisama Год назад
Pls bb, let me put just the type there.
@acryliq
@acryliq 10 месяцев назад
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.
@gro967
@gro967 10 месяцев назад
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
@shapelessed
@shapelessed Год назад
Why would I not put my types in a normal module like everybody else? Let's take my latest project as an example, a file server which builds a virtual file system on top of the one provided by the host OS. It supports linux, mac and windows, but they all handle files and file paths differently. That's why I have multiple classes that handle drive partition and folder access for their specific operating systems, but I also need to have an interface they all implement in order to have a consistent and reliable API.
@duke605
@duke605 Год назад
I’m sending this to a department at my company I often butt heads with. They love putting types in a declaration file and I hate it. Put types where they’re used. I didn’t know the typescript team specifically calls this nonsense out. But now I have ammo for why they should stop doing that
@william3588
@william3588 Год назад
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
@MrBrax
@MrBrax Год назад
Thanks, will change my project structures now and credit this!
@Elefantoche
@Elefantoche Год назад
This is what happens when devs jump into codebases with little to no knowledge/context of what they are looking to. They'll try to figure out things doing some "reverse engineering" that may lead to wrong interpretations.
@SamInNZ
@SamInNZ Год назад
Would you advise against using d.ts to store global type helpers?
@mattpocockuk
@mattpocockuk Год назад
No - globals is what .d.ts files are for!
@hyperprotagonist
@hyperprotagonist Год назад
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".
Далее
Generics: The most intimidating TypeScript feature
18:19
Enums considered harmful
9:23
Просмотров 206 тыс.
any vs unknown vs never: TypeScript demystified
8:01
as const: the most underrated TypeScript feature
5:38
Просмотров 119 тыс.
What's wrong with TypeScript Namespaces?
33:39
Просмотров 1,2 тыс.
Infer is easier than you think
13:38
Просмотров 90 тыс.
The New Option and Result Types of C#
15:05
Просмотров 69 тыс.
TypeScript: tsconfig demystified!
22:32
Просмотров 13 тыс.
7 Awesome TypeScript Types You Should Know
8:57
Просмотров 84 тыс.
Type your functions in TypeScript and SAVE TIME
8:31