remidej
remidej

Reputation: 1645

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"
]
}

Upvotes: 21

Views: 63993

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250106

TypeScript has a defined interface 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) {
    
}

Source: microsoft/TypeScript

Upvotes: 32

Related Questions