You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTMLInputElement type "file" change event.target type (EventTarget) has no files. #31816
constfileInput=document.createElement('input');fileInput.type='file';fileInput.addEventListener('change',(event: Event)=>{handleFileLoad(event.target.files);// Error: files does not exist on EventTarget.});document.body.appendChild(this.fileInput);
changed the title [-]HTMLInputElement type "file" change event type?[/-][+]HTMLInputElement type "file" change event.target type (EventTarget) has no files.[/+]on Jun 7, 2019
document.getElementById("customimage").onchange= function(e: Event) {
let file = (<HTMLInputElement>e.target).files[0];
//rest of your code...
}
Reacted by Sergei Zebzeev, Matt Chaney, João Pitrez, max-lt, Daniel Joachim Larsen, Vlad Nicula, lkiarest, Tomáš H., Taichi Murakami, Fatih Sinan Yaman and 20 more
Reacted by doox911, Iinguistics, Arsen Batyuchok, codmitu, soulan, Kristian Roebuck, Shantanu Sharma, Salim Menari, 思来享趣, Dmitry Morozov and 7 more
Reacted by Daniel Demečko, Brian Kim, Luciano Lima, Prasaanth Sridharan, Sergei Zebzeev, Cristafovici Dan, Adel and Oleg
Reacted by Finnian Anderson, Thilo Ilg, Amit Beckenstein, Luciano Lima, Ilya Borisov, Slim, michael trieu, Marvin Heilemann, Chris Sun, Andrew Vitale and 15 more
This can be made backwards compatible with default generics:
// still worksfunctionmyEventListener(event: Event){}
Reacted by Serhii Atamaniuk, Matthew MacLeod, Andrew D., John Colella, Vasyl Mazur, Alfonso Andrés López Molina, Anton Minin, Jakob Yoo, Jake, peetzweg/ and 53 more
Reacted by Владимир
Reacted by KytoSai, Gabriel Santos, Vladislav, khkim-koodon, Matthijs Kralt, Juan Cruz, AMANI Eric, Dima Morar, Kevin Deng, Jakub Strzadala and 16 more
Reacted by Igor Nikiforov, M.Hosoi, freemarketsoftware, Marcos Adriano, Julius Kalis, Kacper Biedka, SWAPNIL SHARMA, Devon, SetMod, Fabio Di Stasio and 22 more
Reacted by Timothy Okooboh, priests123 and DucH1211
That's correct. It also exists in TypeScript 3.4.5. It also exists in the old version TypeScript 2.0
I don't know what I was thinking back then. ... 🤦♂️
I close this issue as fixed. Thanks. The type HTMLInputElement has the property files which is nullable FileList. All fine.
Reacted by infacto, Tsvetan Ganev, Andrei Victor Bulearca, John Theodorakopoulos, Lucas Avendaño, Bohdan Anderson, Gustavo Marquez Prieto, Tord Standnes, Jeluxe and Baptiste Fontaine
I understood the issue as event.target doesn't have files and that's because it requires casting.
We could take this opportunity to remove the casting step using generics
Means that the HTMLInputElement is detected and maybe available for a generic event type. 🤔
Is there already a generic Event type? But anyway it is not resolved in this generic type.
It's a good idea. If TypeScript detects the context of the event listener - in this case - why not?
But note that in my initial post, I use the fixed type Event for the event variable.
So the Event type of a addEventListener should detect the target type (generic) if possible.
Any examples or ideas? Any expertise of a member or constributor?
function handleFileChange(event: React.ChangeEvent<HTMLInputElement>) {
console.log(event.target.files);
}
Reacted by Алексей Ульянов, Fernando Zabin, Bruno Macabeus, Jules Blom, Sergio Sanchez, Ville Saukkonen, Jesper Paulsen, Ivan, Vladimir Kuzmin, JMDP and 27 more
Reacted by Zhang Yi Jiang, Danny Goncalves, Leon Heess, 0xAnmol, Andrea Taglia, Mizan Rifat, Andrew Lam, VladimirAngelov, Vincent Rubinetti and Hidayt Rahmani
Reacted by Fernando Zabin, Bruno Macabeus, Ville Saukkonen, Jesper Paulsen, lucas-leal-develcode, Rainério Costa, GTOsss and Abílio Soares Coelho
Reacted by Vladislav, Nikita M, Santiago Molina, Brendan, Алексей Ульянов, Fernando Zabin, Bruno Macabeus, Isaias Morona, Ville Saukkonen, Jesper Paulsen and 8 more
Reacted by Rainério Costa, GTOsss and Abílio Soares Coelho
React.ChangeEvent<HTMLInputElement> this doesn't work in case of filelist. It gives the error: Type 'FileList' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators
Activity
[-]HTMLInputElement type "file" change event type?[/-][+]HTMLInputElement type "file" change event.target type (EventTarget) has no files.[/+]leonelngande commentedon Jan 9, 2020
As per this Stackoverflow answer, you can cast
event.targetasHTMLInputElementto get around this.DDeme commentedon Jan 20, 2020
I suggest extending EventTarget for optional property files.
ZackDeRose commentedon Mar 1, 2020
For those that come behind, I liked this solution:
amatiasq commentedon Jun 1, 2020
I always wondered why
Eventis not generic.So we define the type where we need it.
This can be made backwards compatible with default generics:
infacto commentedon Jun 18, 2020
I'm working with TypeScript
~3.8.3andHTMLInputElementcontains nowfiles(FileList).See sources 👍
Domvel commentedon Jun 19, 2020
That's correct. It also exists in TypeScript 3.4.5. It also exists in the old version TypeScript 2.0
I don't know what I was thinking back then. ... 🤦♂️
I close this issue as fixed. Thanks. The type
HTMLInputElementhas the propertyfileswhich is nullableFileList. All fine.amatiasq commentedon Jun 19, 2020
I understood the issue as
event.targetdoesn't have files and that's because it requires casting.We could take this opportunity to remove the casting step using generics
Domvel commentedon Jun 19, 2020
This is acual resolved by the TypeScript in VS Code. (just hover the method to see it.)
Means that the HTMLInputElement is detected and maybe available for a generic event type. 🤔
Is there already a generic
Eventtype? But anyway it is not resolved in this generic type.It's a good idea. If TypeScript detects the context of the event listener - in this case - why not?
But note that in my initial post, I use the fixed type
Eventfor theeventvariable.So the
Eventtype of aaddEventListenershould detect the target type (generic) if possible.Any examples or ideas? Any expertise of a member or constributor?
1 remaining item
phfoxer commentedon Sep 7, 2020
egorovsa commentedon Mar 12, 2021
Just
egorovsa commentedon Nov 7, 2021
Just check it through: "?"
const file = target.files?.[0];however pay attention that the type will be File | undefined
stevenKirill commentedon Nov 7, 2021
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
Dassderdie commentedon Nov 7, 2021
Kirill (@stevenKirill)
You could use the non-null-assertion-operator-postfix, like this
const file = target.files![0];if you are completely sure thatfilesis defined.hejuhenryk commentedon Dec 3, 2021
then check if it is not
if (e.target.files && e.target.files.length > 0) { const file = e.target.files[0]ritikbanger commentedon Feb 8, 2022
React.ChangeEvent<HTMLInputElement>this doesn't work in case offilelist. It gives the error: Type 'FileList' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iteratorsumbrashia commentedon Nov 17, 2022
use "Types" for solution of that problem
export type TGenericEvent<T> = Event & { target: T };use like this
AntonyStone1 commentedon Dec 25, 2022
guiled commentedon Feb 12, 2023
Shantanu Sharma (@umbrashia) and A. Matías Quezada (@amatiasq) suggestion should be implemented natively in ts
rcollette commentedon Feb 13, 2023
Casting isn't helpful for unit tests. Having the Event type be a generic would be more useful.
vincerubinetti commentedon Jun 2, 2023
I ended up having to do this:
🤪
KraXen72 commentedon Aug 11, 2023
after many iterations, this is what worked for me:
i'm on
typescript@^5.1.6, using svelte'son:change={upload}but unsure if that's relevantcassepipe commentedon Aug 14, 2023
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