Does ts-reset fix TypeScript?

Поділитися
Вставка
  • Опубліковано 20 лют 2023
  • I might have fixed TypeScript - or at least, fixed the things I hate most about it - with a library called ts-reset.
    Yes, that tweet in the thumbnail is real: / 1627713574553387025
    Repo: github.com/total-typescript/t...
    Become a TypeScript Wizard with Matt's beginners TypeScript Course:
    www.totaltypescript.com/tutor...
    Follow Matt on Twitter
    / mattpocockuk
  • Наука та технологія

КОМЕНТАРІ • 226

  • @levantos
    @levantos Рік тому +228

    They could always add as an option in the tsconfig to opt-in rather than as a baseline

    • @echoptic775
      @echoptic775 Рік тому +6

      Add stricter mode

    • @cacup7
      @cacup7 Рік тому

      @@echoptic775 Or even an option that forces unkown instead of any to these cases pointed out by Matt's video.
      I Believe that would help keep legacy mode with the option to be type safer.

    • @kibiz0r
      @kibiz0r Рік тому

      @@echoptic775 Or "strict": "5.1", to let the maximum strictness expand over time without breaking backwards compatibility. "strict": true would just start you at "5.0".

  • @cask2cask
    @cask2cask Рік тому +92

    The Boolean filter keeping falsy values in the type def kills me, this is amazing and will definitely be appreciated by my team

    • @chrisvouga8832
      @chrisvouga8832 Рік тому +4

      If you can't use ts-reset, another way to filter arrays with proper type inference is to use .flatMap instead of .filter
      const arr = ["a", "b", "c", undefined];
      // arr: (string | undefined)[]
      const filteredArr = arr.flatMap(item => item ? [item] : [])
      // filteredArr: string[]

    • @Seedzification
      @Seedzification Рік тому

      please dont die

    • @82TheKnocKY
      @82TheKnocKY Рік тому +3

      @@chrisvouga8832 If you pass a function with the return type containing 'is' you can achieve the same result

    • @vickmackey24
      @vickmackey24 Рік тому +1

      @@chrisvouga8832 You'd have to tweak that conditional a bit if empty strings are allowed.

    • @healqq
      @healqq 11 місяців тому +1

      as @82TheKnocKY mentioned - you can use type predicate to achieve same effect

  • @maxijonson
    @maxijonson Рік тому +72

    I'm scared to see how many errors will pop up after I install this, which also makes me excited to use it 😂 You know you're a TS programmer when you're happy to see red squiggles in your editor. "Ahhhh yes... type safety!"

  • @AlecMaly
    @AlecMaly Рік тому +29

    You’ve presented the concept and examples really well, great work!

  • @bobbymannino
    @bobbymannino Рік тому +28

    Doing gods work 🙏

  • @mahadevovnl
    @mahadevovnl Рік тому +9

    I like this. Being the tech lead for a big international company, I'm about to start implementing TS for our primary business software together with Svelte. It's going to be added, for sure! Thanks!

  • @nonudian
    @nonudian Рік тому +1

    That's sooo awesome and ingenious, really cool work !

  • @mattzeepk2993
    @mattzeepk2993 Рік тому +2

    This is great! I was surprised that you nailed 2 examples that I've tried to deal with multiple times lol

  • @ionut-cristianratoi7692
    @ionut-cristianratoi7692 Рік тому +1

    This is amazing! Thank you for this library!

  • @Palundrium
    @Palundrium Рік тому +1

    Wild, saw ts-reset on the TL;DR newsletter, no idea it was from this fine gentleman. Keep on rocking!

  • @elrmndo
    @elrmndo Рік тому +1

    This is cool Matt. Thanks!

  • @JEsterCW
    @JEsterCW Рік тому +1

    Matt, youre the biggest *gigachad* over here and there, lovely work buddy!

  • @kennethkwakye-gyamfi4765
    @kennethkwakye-gyamfi4765 Рік тому +1

    You had me at "Hello"

  • @hrimthurs88
    @hrimthurs88 Рік тому +1

    I am so psyched! 😊

  • @rahulxcr
    @rahulxcr 11 місяців тому

    This is really helpful library. Thanks.

  • @erikplachta
    @erikplachta Рік тому +7

    Really cool, nice work Matt.
    Found a few spots in my API where I was missing guard statements.
    Love that it only needs to be imported once ❤

  • @VeitLehmann
    @VeitLehmann Рік тому +1

    This is skyrocketing! Maybe because it's a skyrocket? This is huge, thanks so much for this!

  • @mattd5419
    @mattd5419 Рік тому

    I love this! you are a life saver ❤

  • @lokenathkarmakar
    @lokenathkarmakar Рік тому +1

    love this filter one

  • @MaxProgramming
    @MaxProgramming Рік тому +2

    I still believe you fixed TypeScript by doing this. You are a genius Mattie 🎉

  • @johannes.przymusinski
    @johannes.przymusinski Рік тому +2

    Actually … I today had an, let say „augment“ with typescript for not correctly typing a filtered array, removing all null values. That exactly what you showed as your favourite example. I know what I’ll check out tomorrow at work and see what else it helps me there :)

  • @blueghost512
    @blueghost512 Рік тому +3

    Best clickbait ever.
    I was wondering if this lib can include some utility types which are not included in TS, like “AwaitedReturnTypeany> = Awaited“ and “Exact = {[K in keyof T]: K[T]}“

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      Sounds like it would belong best in a different lib!

  • @ibnu7942
    @ibnu7942 Рік тому +1

    that's it
    finally i don't need to typecast my filtered array anymore

  • @siulingding73
    @siulingding73 Рік тому +1

    oh thank you for making this lib!!

  • @alexanderkomanov4151
    @alexanderkomanov4151 Рік тому +1

    Cool Explanation!

  • @Arzen84
    @Arzen84 Рік тому +1

    Mate… You are amazing!

  • @feldinho
    @feldinho Рік тому +5

    coming from PHP, where the @organization is mandatory (on composer), I find it great! this way it's easier to check the provenance of a package without digging through npm, adding a layer of safety and maybe even accountability.

    • @seannewell397
      @seannewell397 Рік тому

      When things are open source and you can pin the versions, don't know why this matters. Smells of audits and bureaucracy. I understand ISO/SOC, but I also understand I have precious little time and need to work on the most valuable thing and ship value to customers. And audits get in the way of that, so I try to do the bare minimum and automate as much as possible @ work so it's minimal pain once a year.

    • @feldinho
      @feldinho Рік тому

      @@seannewell397 I don't know how you went from naming conventions to audits. Maybe I wasn't so clear in my point? I only said that Composer (npm for php) uses a two-part naming system, so you always know what is the person or organization behind a package. Typing "@react/react" or "@facebook/react" is slower than typing "react", but not by much…

  • @aprilmintacpineda2713
    @aprilmintacpineda2713 Рік тому +1

    I love this!

  •  Рік тому +37

    For 'any' I typically rely on eslint no-unsafe-* rules so that is normally not an issue for me. Filtering collections and having the correct collection type is a great thing. However I'm not sure I would like to include an additional package just for that. Great work in any case.

    • @AndersGustafsson87
      @AndersGustafsson87 Рік тому +7

      hehe same, although this "package" is literally only some type definitions for development ergonomics, does not affect actual running code at all

    • @mattpocockuk
      @mattpocockuk  Рік тому +6

      Agree - though I think this solution would run faster because it doesn't rely on re-running/re-querying tsserver. So if you're noticing a slowdown using the no-unsafe-* rules, this might be a useful alternative.

  • @gregorysk_lviv
    @gregorysk_lviv Рік тому +1

    Cool, I will check in my project

  • @AlexEscalante
    @AlexEscalante Рік тому +3

    You released this today and it has 1.8k stars in Github? Legend!

  • @rodrigoazevedo8301
    @rodrigoazevedo8301 Рік тому +1

    The Array filter typing 🤩

  • @Wizarth
    @Wizarth Рік тому +3

    I'd love to see a revised version of the normal libs, or some related flag, that revises all the places where any was used before unknown was added to the language.
    It also seems this might work as a types library (except for that annoying behavior that specifying types at all removes the default behavior).
    It just seems to be this is more a compiler targeted library than a "include this in your code" library.

  • @SR-ti6jj
    @SR-ti6jj Рік тому +4

    MS devs are trembling at the TS wizard's power

  • @JamesQQuick
    @JamesQQuick Рік тому +1

    Wow, that's super interesting!!

  • @21Blankenship
    @21Blankenship Рік тому +1

    The filter bit would be very helpful, this is a very common issue for me

  • @driverjb09
    @driverjb09 Рік тому

    I've started applying zod schemas around my JSON.parse calls so what comes out is parsed, validated and typed all at once.

  • @GustavoDiaz93
    @GustavoDiaz93 Рік тому +1

    True TS wizard at working 🧙‍♂️

  • @toddjudd9552
    @toddjudd9552 Рік тому +1

    Yes!! Just YES!

  • @raymondyoo5461
    @raymondyoo5461 Рік тому

    Wow you are really a typescript wizard 👍👍

  • @eddiedotdev
    @eddiedotdev Рік тому

    If I want to buy your Total Typescript course for my team at work, do you have some sort of bulk discount if I buy a certain number of licenses?

  • @nikensss
    @nikensss Рік тому +1

    oh, if it only needs to be imported once I'm definitely adding it tomorrow!!!

  • @kuroisan2698
    @kuroisan2698 Рік тому

    Will TS be understood by the engines like V8 ( and the browser ) in the future ?

  • @adamwheatley3372
    @adamwheatley3372 Рік тому +10

    I really like the changes. Personally, I am not sure it includes enough to warrant a full package and would maybe rather a copy/paste approach but that may just be due to me already having an ungodly amount of dependencies to manage already

    • @8koi245
      @8koi245 Рік тому +2

      hell yeah the deps section of mine is almost the size of the screen...

    • @soviut303
      @soviut303 Рік тому +1

      This is a dev dependency, though. It won't get bundled into your code so it doesn't really matter how many of those you have. Copying and pasting is still creating a dependency, it's just doing it locally and means you won't have the option to update it via your package manager when new changes come out.

    • @adamwheatley3372
      @adamwheatley3372 Рік тому

      @@soviut303 it doesn’t really matter how many isn’t exactly true though is it even if they are dev dependencies. Especially when working on large codebases there is definitely a cost on how many packages you have to manage, upgrade, etc. also, your point about still creating a dependency by copy/pasting I don’t really understand, it’s no more a dependency than other LOC in your project reality.

    • @soviut303
      @soviut303 Рік тому

      @@adamwheatley3372 Dependencies come with documentation, tests and escape hatches. Code someone randomly copied into a project usually has none of those things and is now embedded with no support once that person leaves. You're now stuck with legacy code that you have to maintain, which is one more perpetual distraction from the actual problem your software is supposed to be solving.

    • @adamwheatley3372
      @adamwheatley3372 Рік тому

      @@soviut303 my whole point was that the code in this package doesn’t warrant a full package. These types don’t have tests, document themselves, and don’t require an escape hatch. Your argument is basically all code is legacy code if it’s not in a dependency, sadly in real life you need code!

  • @pupfriend
    @pupfriend Рік тому

    2:34 OMG!!!!! Thank you!!!

  • @goncalomarques7502
    @goncalomarques7502 Рік тому

    How did you make it so just importing once it expands to the whole project?

  • @soviut303
    @soviut303 Рік тому +2

    I personally prefer the org namespace on the module import.

  • @anonymousBl1
    @anonymousBl1 Рік тому

    Any chance you can fix the "Property x does not exist on type EventTarget" error? 😊

  • @wfl-junior
    @wfl-junior Рік тому +2

    Can you add the Object keys to be (keyof Obj)[] instead of string[]? And also the object entries

    • @mattpocockuk
      @mattpocockuk  Рік тому +2

      Nope! Check the readme for why

    • @wfl-junior
      @wfl-junior Рік тому

      @@mattpocockuk I see, it is a good point.

  • @gabrielrochadeoliveira6929
    @gabrielrochadeoliveira6929 Рік тому +2

    Hi, @Matt Pocock
    Could you please take a look on a library I've been working with these last months?
    It's called ts-narrow, documentation is kinda bad for now, but you can have a pretty good idea on how it works from the unit tests inside it.
    Thank you for your attention anyways!

  • @user-ev2rn8gp8i
    @user-ev2rn8gp8i 6 місяців тому

    Dear sir, I love your contents. Thank you so much.
    I wonder about the "is-array" rule, instead of unknown[] wouldn't ReadonlyArray be a better type fix? It seems otherwise mutable methods such as push, that were prohibited by the type declaration, would be allowed again.

  • @seanki
    @seanki Рік тому +1

    Cool lib!

  • @nrrgrdn
    @nrrgrdn Рік тому

    What about just eslint no implicit any?

  • @pawekoaczynski4505
    @pawekoaczynski4505 Рік тому +1

    2:25 - Yes! This little thing has been buggin me for some time. Thanks :p

  • @mt1104uk
    @mt1104uk Рік тому +3

    NPM Organisations should be mandatory. Over the years so many NPM packages have just died or the maintainers have given up. Having all packages scoped to an organisation makes forking a project have much less friction.
    As it stands, the "global" packages (for the lack of the better word) will always take precedence in the minds of developers.

  • @j.m.manhard
    @j.m.manhard Рік тому +1

    Filter is a good start, can you make it work for reduce too?

  • @lucasa8710
    @lucasa8710 Рік тому

    when you type "// ^?" a type hint appears on the screen. What is that? is it an extension?

  • @thechoephix
    @thechoephix Рік тому +1

  • @pavolhejny
    @pavolhejny Рік тому +1

    ❤ Great! I probbably should be sleeping right now (greetings from Prague; 3:41AM) BUT I have discovered this project and must include into my projects.

  • @AdamJohnson74
    @AdamJohnson74 Рік тому +1

    what's the extention that's showing types?

    • @mattpocockuk
      @mattpocockuk  Рік тому

      ua-cam.com/video/u0adKDu--cA/v-deo.html

  • @vlad.nicula
    @vlad.nicula Рік тому

    Is there a way to get the "// ^?" in VS Code by any chance? It would be amazing

  • @tiagoernst5167
    @tiagoernst5167 Рік тому

    Would there be any downsides regarding performance? I guess for the JSON parse you're just changing the return type but not sure how the filter is actually done

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      I don't think so - for many of these I'm simply adding another overload, which is easy for TS to match.

  • @dinoscheidt
    @dinoscheidt Рік тому +1

    1:01 😮 ….. nervously looking at codebase

  • @GianlucaCasati
    @GianlucaCasati Рік тому

    also test utils in ts-reset repo are interesting… some of them could be included in TS, for example Expect

  • @gosnooky
    @gosnooky Рік тому

    Looked at the repo - and to clarify, if you add interface definitions with differing signatures, it adds overloads to the original definition?

  • @bigmistqke
    @bigmistqke Рік тому

    Already saw it passing by on gh and ✨ it. Great project 👍 It's a bit scary it changes the global types, reminiscent of monkeypatching, but since the changes are conservative I am fine with it? Kinda hope (at least some of) ur changes could be upstreamed, because they just more accurately describe the types imo. I sort of assume that array.filter would be too heavy on performance to have it in official ts, but things like the JSON parsing to unknown instead of any just feels right and shouldn't have any impact.

  • @rafaeltab
    @rafaeltab Рік тому

    now this is poggers

  • @MK-bx8xv
    @MK-bx8xv Рік тому

    Are you using an extension to see the actual type in the comment line?

  • @Mitsunee_
    @Mitsunee_ Рік тому +1

    I think I'd love having a generic that defaults to unknown, so if I know the json I'm parsing (or have error boundaries to catch stuff) I can just get the type attached right away without zod. What really badly needs fixing tho are the static Object methods. What good is string[] as the return value for Object.keys for Records of specific keys. Keep having to manually type those, defeats the entire purpose of using TypeScript moreso than what I do with readFileJson and readFileYaml...

    • @bigmistqke
      @bigmistqke Рік тому

      Object.keys is explained in the GitHub page of ts-reset. To paraphrase: ts does structural typing, meaning that an interface/type can describe the key-values that HAS to be in the object, but it can not describe the type that SHOULD NOT be in the object. A union of strings on the other hand, does describe all the possible strings that are valid. So Object.keys could return more strings then the keys of the type/interface that is describing the object, and it would still be considered 'correctly typed'. Hope this makes sense.

  • @hiramhernandezpena889
    @hiramhernandezpena889 Рік тому

    The best!!!

  • @gorimor
    @gorimor Рік тому +1

    God that's great

  • @loucadufault6549
    @loucadufault6549 Рік тому +5

    Always snag the npm namespace first, ask questions later ;) I even made a package that does just that (hold-place).
    Also a suggestion, I would personally be more likely to use this if it were vended as a plugin/extension system rather than the all-or-nothing approach. It's hard to justify bringing it some external code that will fundamentally alter the behaviour of TS unless each modification is explicitly declared.

  • @blaketheory
    @blaketheory Рік тому

    This is neat, I'm gonna have to give it a try. At first I thought it was doing the same as noImplicitAny in tsconfig, but that's different since things like JSON.parse explicitly return any, even if you don't notice it.

  • @levantos
    @levantos Рік тому +1

    Here we go :D

  • @willkuerlich
    @willkuerlich Рік тому +1

    I have been waiting for the typesafe filter function all my life, super annoying that TS can't infer it by itself

  • @studiowebselect
    @studiowebselect Рік тому

    Is it possible to have it in an Library and dont ship it in the bundle?

  • @KirkWaiblinger
    @KirkWaiblinger Рік тому

    It would be interesting if there were a way to do this more generally, not just in one-off situations. For instance axios returns any by default too, unless you manually specify the generic. I don't know of a good way to wield the ts compiler or eslint to do this unfortunately.
    It seems though that banning assigning the result of a function call to any without a type annotation might get us a long way. Or perhaps there could just be a TS option that makes any function returning any return unknown instead.
    Banning any altogether seems misguided, but the cases where you silently get any as a result of a third party function call seem generally pretty harmful.

  • @magne6049
    @magne6049 Рік тому

    1:16 hmm what about «parse, don’t validate»?

  • @GaurangRShah
    @GaurangRShah Рік тому +2

    What tool are you using to get those comment previews of your types with "^?"

    • @BBr0wn
      @BBr0wn Рік тому

      I'm also interested...

    • @kevingeorget6002
      @kevingeorget6002 Рік тому +2

      It's a feature of Typescript Playground. But i use vscode-twoslash-queries extension 🙂

    • @GaurangRShah
      @GaurangRShah Рік тому +1

      @@kevingeorget6002 thanks! This is a life saver!

  • @Cangussu97
    @Cangussu97 Рік тому

    Awesome! Great tool for developers so safeguard their development.
    Is the "^?" To display type annotations a default os VS Code or is it from an extension?

  • @cd-stephen
    @cd-stephen Рік тому +1

    new sub thanks to primigen

  • @RicardoValero95
    @RicardoValero95 Рік тому

    Could you disable all global imports? 😬

  • @juanmanuelvelez3571
    @juanmanuelvelez3571 Рік тому

    a reset for window would be amazing, now days a lot of code is server render, where window is "undefined" yet when you write the code the IDE always pops the error where window can not be undefined when you do the classical if(window===undefined)

    • @Mitsunee_
      @Mitsunee_ Рік тому

      I usually do `typeof window !== "undefined"` to check that I'm running in the client

  • @romanmatveev314
    @romanmatveev314 Рік тому

    i do not use ts or js, btw it is really cool changes for production

  • @tobiasweyer5063
    @tobiasweyer5063 Рік тому

    can someone explain to me, how those comments '^?' work in vscode? Is this an extension?

  • @wtbgothgf
    @wtbgothgf Рік тому

    Can someone tell me how he is getting the type of that particular variable autofilled? with the // ^? comment? Is this copilot? For me it just gives a random comment.

    • @johnflux1
      @johnflux1 Рік тому

      ua-cam.com/video/u0adKDu--cA/v-deo.html

  • @balduin_b4334
    @balduin_b4334 Рік тому +1

    i am afraid to do this as an experiment in my project...

  • @Just_Moh_it
    @Just_Moh_it Рік тому +3

    This is awesome, and can’t wait to use this!
    Btw, how’s it going to be used inside real world apps like NextJS repos, and what do you recommend?
    Import inside _document.tsx, inside next.config.js, or somewhere else?

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      Anywhere! Doesn't matter a jot.

    • @Just_Moh_it
      @Just_Moh_it Рік тому +1

      @@mattpocockuk That's great! Though, where do you suggest it to be used in Next.js, so that it won't matter even if the project structure is changed significantly?

    • @mattpocockuk
      @mattpocockuk  Рік тому +2

      @@Just_Moh_it Won't matter at all! _app or _document feel natural.

  • @hicoop
    @hicoop Рік тому

    This is so cool but is there a way to automatically import this in all typescript files?

    • @mattpocockuk
      @mattpocockuk  Рік тому

      Yes, a single import will make it go through the entire codebase.

    • @JEsterCW
      @JEsterCW Рік тому

      @@mattpocockuk So it covers global types even for tsx files etc? thats cool so its like overwritting the global types, lovely!

  • @f4pq4bt
    @f4pq4bt Рік тому

    Nice job!!!
    Is it possible to fix the Object.keys to be more type safe? :D
    Example:
    const a = { aa: 1, bb: 2, cc: 3 } as const;
    const values = Object.values(a); // (1 | 2 | 3)[]
    const keys = Object.keys(a); // string[] expected: ("aa" | "bb" | "cc")[]

  • @Gabriel-iq6ug
    @Gabriel-iq6ug Рік тому

    Matt will be live coding in 10 minutes guys!

  • @Gabriel-iq6ug
    @Gabriel-iq6ug Рік тому

    thanks, these were the 2 things that bother me the most

  • @archmad
    @archmad Рік тому +1

    why not add it to tsconfig as plugin?

    • @mattpocockuk
      @mattpocockuk  Рік тому +1

      Because plugins don't contribute to typechecking.

  • @8koi245
    @8koi245 Рік тому

    HELL YEAH I FOUND IT AGAIN

  • @RobertKonigsberg
    @RobertKonigsberg Рік тому +1

    That was good clickbait.

  • @nomadshiba
    @nomadshiba Рік тому

    YES

  • @StiekemeHenk
    @StiekemeHenk Рік тому

    What's the plugin that shows the type in your comment

  • @michaeldausmann6736
    @michaeldausmann6736 Рік тому

    How is this different from using typescript in strict mode and using the noImplicitAny flag?

    • @mattpocockuk
      @mattpocockuk  Рік тому

      All of my examples show TypeScript in strict mode, including the ones with any's coming from JSON.parse.

  • @MaxPicAxe
    @MaxPicAxe Рік тому

    Shouldn't a library be able to opt-in in an isolated way for itself? Might be difficult to implement but could be nice. I find it's a bit limiting to opt-in at an "application level" as I think library boundaries can be quite arbitrary, for example, in a team, someone may opt-in but then wants to move their code to a library for another part of the team, and then this part of the team doesn't want to opt-in for their part of the code. So I think the global opt-in is a bit limiting, but I'd imagine it would be very challenging to implement isolated opt-in.

  • @Tijme
    @Tijme Рік тому +1

    Great