Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create File object type in TypeScript

I have a function that should only accept JS File objects.

How do I create that type in TypeScript?

edit: sharing my tsconfig.json

{
"compilerOptions": {
    "outDir": "./ts-dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es6",
    "jsx": "react",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "lib": [
        "dom",
        "es2017"
    ]
},
"include": [
    "./src/**/*", "./test/**/*"
],
"exclude": [
    "node_modules", "dist"
]
}
like image 591
remidej Avatar asked Aug 07 '18 08:08

remidej


People also ask

How do you create a file object?

When we initialize File class object, we provide the file name and then we can call createNewFile() method of the File class to create a new file in Java. The File. createNewFile() method throws java. io.

How do you create a object in TypeScript?

Syntax. var object_name = { key1: “value1”, //scalar value key2: “value”, key3: function() { //functions }, key4:[“content1”, “content2”] //collection }; As shown above, an object can contain scalar values, functions and structures like arrays and tuples.

What is the file type in TypeScript?

TypeScript has two main kinds of files. . ts files are implementation files that contain types and executable code. These are the files that produce . js outputs, and are where you'd normally write your code.

What is object type in TypeScript?

In TypeScript, object is the type of all non-primitive values (primitive values are undefined , null , booleans, numbers, bigints, strings). With this type, we can't access any properties of a value.


1 Answers

Typescript has a defined type for File which represents the file object in JavaScript. Generally you should look for types named as their Javascript countertypes. You can use this type in a parameter annotation for the parameter:

function processFile(file: File) {

}
like image 179
Titian Cernicova-Dragomir Avatar answered Sep 26 '22 00:09

Titian Cernicova-Dragomir