Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTransferItem Discriminated Union #3

Open
Offroaders123 opened this issue May 21, 2023 · 1 comment
Open

DataTransferItem Discriminated Union #3

Offroaders123 opened this issue May 21, 2023 · 1 comment

Comments

@Offroaders123
Copy link

Realized how to use this in my own projects recently, and now I see that this is what I was looking for a while back to have for the DataTransferItem type. It's API is set up to work great with discriminated unions, but for some reason the standard library doesn't discern the different values against the kind property, while it does mention it in the JSDoc comments also.

DataTransferItem Kind Validation

I made a demo to try and add this functionality myself, but the types don't seem to apply for some reason. It might be that I'm trying to re-assign DataTransferItem with a type declaration instead of an interface one, I'm not completely sure. Either way, this is essentially the shape of what it would look like to enable the discriminated union checking using the kind attribute on the DataTransferItem object.

type DataTransferItem = DataTransferFile | DataTransferString;

interface DataTransferFile {
  readonly kind: "file";
  readonly type: string;
  getAsFile(): File;
  getAsString(callback: null): void;
  webkitGetAsEntry(): FileSystemEntry | null;
}

interface DataTransferString {
  readonly kind: "string";
  readonly type: string;
  getAsFile(): null;
  getAsString(callback: FunctionStringCallback): void;
  webkitGetAsEntry(): null;
}

https://basarat.gitbook.io/typescript/type-system/discriminated-unions

The base type definitions for DataTransfer and it's related APIs are in the DOM API types lib (lib.dom.d.ts), so I think it also may not be applying because of that, since I'm using declare global {} to wrap my custom types. I haven't tried overriding types in other lib definitions yet, so I think it also could be that.

Base DataTransferItem Lib Types
Offroaders123 added a commit to Offroaders123/Dovetail that referenced this issue May 21, 2023
Added some more established function return types, and looked into additional type checking capabilities for the `DataTransferItem` type.

BenjaminAster/Better-TypeScript#3

Had the Service Worker TypeScript-ready again, but realized that would be a bit too much to change all at once, and I didn't want to implement too many things to where it would break something unexpectedly again.
@BenjaminAster
Copy link
Owner

I don't really think it's possible to completely reassign existing types and interfaces in TypeScript; at least not without causing some error messages in either lib.dom.d.ts or in the custom ones. If you can somehow get it to work, show me and I can add it to Better-TypeScript.

Offroaders123 added a commit to Offroaders123/Dovetail that referenced this issue Jul 7, 2023
Currently TypeScript doesn't have a way to distinguish between if a `DataTransferItem` has a file or a string (at the type level). This is my proper fallback to get this behavior working.

It doesn't seem to be possible to override the types from `lib.dom.d.ts`, is it?

BenjaminAster/Better-TypeScript#3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants