2

I am writing a web application using Angular 4 and Typescript. I need the date of a file to upload and try to use the File objects lastModified property, but Typescript gives me an error

Property 'lastModified' does not exist on type 'File'.

If I look in the definition it instead have the lastModifiedDate as a property. According to https://developer.mozilla.org/en-US/docs/Web/API/File/lastModifiedDate that property is depriciated. I have however tried it and it works in Chrome, but fails in Safari.

How can I use File lastModified property from Typescript?

3
  • Please provide peace of code for people to see if you are doing it right. Commented Nov 1, 2017 at 20:04
  • But File.lastModified does not exist in lib.dom.d.ts: interface: File extends Blob { readonly lastModifiedDate: any; readonly name: string; readonly webkitRelativePath: string; } Commented Nov 1, 2017 at 20:08
  • github.com/Microsoft/TypeScript/issues/16942 Commented Nov 1, 2017 at 20:16

2 Answers 2

5

Try

interface MyFile extends File {
    lastModified: any;
}

let myFile = <MyFile>originalFile;
let lm = myFile.lastModified;
Sign up to request clarification or add additional context in comments.

Comments

0

Also for lastModifiedDate, which is deprecated but for now still around in Chrome and was the only option in IE:

// use non-deprecated lastModified field instead 
new Date(file.lastModified)


// in-line type assertion
(file as unknown as { lastModifiedDate: Date }).lastModifiedDate

// type assertion with special type
type DateFile = File & {
  lastModifiedDate: Date;
};
...
(file as DateFile).lastModifiedDate

will prevent TS2551 Property 'lastModifiedDate' does not exist on type 'File'. Did you mean 'lastModified'? ts(2551) error by using lastModified instead or asserting that it exists.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.