我看到我的集成开发环境将File识别为Typescript对象。执行refined search in Google会将我带到this page,它与File对象没有任何关系。File既不在basic types中,也不在advanced types中。
Typescript中的File对象是否已处理为exactly the same way as in Javascript?有什么突破性的差异吗?
同样,如何在Typescript中实例化文件类型?
发布于 2019-03-05 06:10:07
File类没有被定义为TypeScript语言本身的一部分,而是DOM规范的一部分。TypeScript为DOM对象提供了一个标准声明文件,作为标准库的一部分,您可以通过here自己查看该文件
/** The File interface provides information about files and allows JavaScript in a web page to access their content. */
interface File extends Blob {
readonly lastModified: number;
readonly name: string;
}
declare var File: {
prototype: File;
new(fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
};当然,声明文件本身对用户并不是很友好。您可能会发现MDN DOM API documentation更有用(但请注意,它是用于JavaScript的,而不是TypeScript的,所以不要期待任何显式的类型注释)。它提供了以下示例:
var file = new File(["foo"], "foo.txt", {
type: "text/plain",
});尽管从技术上讲这是JavaScript,但它可以像TypeScript一样很好地编译,并且推断出的file类型将是File。
https://stackoverflow.com/questions/54992227
复制相似问题