-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
HTMLInputElement type "file" change event.target type (EventTarget) has no files. #31816
Comments
As per this Stackoverflow answer, you can cast
|
I suggest extending EventTarget for optional property files. |
For those that come behind, I liked this solution: export type FileEventTarget = EventTarget & { files: FileList };
const fileInputNativeElement =
fromEvent(fileInputNativeElement, 'change') as Observable<{ target: FileEventTarget }>; |
I always wondered why interface Event<T = EventTarget> {
target: T;
// ...
} So we define the type where we need it. function myEventListener(event: Event<HTMLInputElement>) {
console.log(event.target.files);
}
function otherEventListener(event: Event<HTMLAnchorElement>) {
console.log(event.target.href);
} This can be made backwards compatible with default generics: // still works
function myEventListener(event: Event) {} |
Don't put the type of event :) |
I'm working with TypeScript const target = event.target as HTMLInputElement;
const files = target.files; |
I was have the same problem and it worked when I quit use the type of element. Thank you for answer me. |
That's correct. It also exists in TypeScript 3.4.5. It also exists in the old version TypeScript 2.0 I close this issue as fixed. Thanks. The type const target = event.target as HTMLInputElement;
const files = target.files; |
I understood the issue as |
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.addEventListener('change', event => {
console.log(event.target.files);
}); This is acual resolved by the TypeScript in VS Code. (just hover the method to see it.) HTMLInputElement.addEventListener<"change">(
type: "change",
listener: (this: HTMLInputElement, ev: Event) => any,
options?: boolean | AddEventListenerOptions
): void Means that the HTMLInputElement is detected and maybe available for a generic event type. 🤔 So the |
the issue with making |
|
Just
|
Just check it through: "?" |
How can I tell typescript that I exactly will get a file in this example? Does anybody know?
Is says on this line const file = target.files[0] that object is possibly null |
@stevenKirill |
then check if it is not |
|
|
@umbrashia and @amatiasq suggestion should be implemented natively in ts |
I ended up having to do this: type FileEvent = ChangeEvent<HTMLInputElement> & {
target: EventTarget & { files: FileList };
}; 🤪 |
after many iterations, this is what worked for me: async function upload(e: Event & { currentTarget: EventTarget & HTMLInputElement }) {
if (!e.currentTarget || !e.currentTarget.files) return;
const file = e.currentTarget.files[0];
//...
} i'm on |
It's kind sad to have to track down what the DOM events types are over there : https://github.com/microsoft/TypeScript-DOM-lib-generator/blob/f85d145348fc853bc7fecea5e5925c66a2e0e0b6/inputfiles/addedTypes.jsonc#L193 Only to find out that the event you need does not exist in some github issue |
Is there an event type for the HTMLInputElement change event? Currently is automatically resolved as general
Event
>EventTarget
. Which has nofiles
property. seeFileList
.https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
Example:
tsconfig.json (typescript 3.4.5):
If not, this is a feature request. :) e.g.
The text was updated successfully, but these errors were encountered: