ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

add `structuredclone` support

Open ryoppippi opened this issue 2 years ago • 7 comments

https://developer.mozilla.org/en-US/docs/Web/API/structuredClone structuredclone returnsany, and this is horrible as JSON.parse

I think there are two solution for this.

  • return unknown instead of any
  • return appropriate type which depends on the args

Thanks

ryoppippi avatar Feb 25 '23 21:02 ryoppippi

If you would love to, I'd like to create a PR for this topic!!

ryoppippi avatar Feb 25 '23 21:02 ryoppippi

@ryoppippi Could you do me a favour? Could you dig out in TS's issues the reason why structuredClone returns any and not T? Feels like an obvious candidate to be a generic function, I'd love to know why they didn't go that way.

mattpocock avatar Feb 25 '23 22:02 mattpocock

Related: https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1237 @mattpocock @ryoppippi

Jacob-Lockwood avatar Feb 26 '23 02:02 Jacob-Lockwood

They have a system that automatically generates types from new specs, but I guess it is not advanced enough to support generics (or maybe the spec is not written in a way that gets picked up as a generic function), so it ended up with anys instead. So no-one decided to use any, it was just automatically generated like that, and no-one fixed it yet.

guillaumebrunerie avatar Feb 26 '23 09:02 guillaumebrunerie

In typescript 5.0, structuredclone is fully typed natively. Thank you for this discussion guys!!!

ryoppippi avatar Apr 09 '23 07:04 ryoppippi

@ryoppippi Which version exactly is it typed fully? Both my package.json and VS Code workspace says I'm on version 5.0.2, but the definition of structuredClone in lib.dom.d.ts still uses any:

declare function structuredClone(value: any, options?: StructuredSerializeOptions): any;

Svish avatar Apr 20 '23 09:04 Svish

@Svish I created vanilla vite project and tested, and yeah you are right. structuerdClone still returns any.

However, in Sveltekit project, it works fine tho.

My environment is here

  • Nvim 0.9
  • Nvim-cmp
  • TSServer 3.3.2
  • Typescript 5.0
  • Sveltekit 1.15.5 Here is the screenshot スクリーンショット 2023-04-20 18 01 10

idk why it works. But I think we need to open this issue again, maybe.

Thank you for letting me know this issue @Svish

ryoppippi avatar Apr 20 '23 17:04 ryoppippi