I have got an interface like this
export interface ReportFiles {
ReportName: string;
Log: number;
MyFile: File;
}
And in my component, I declared the object as
export class MyComponent Implements OnInit {
RF: ReportFiles = { ReportName: "", Log: 0, MyFile: null }
...
...
}
The error message shows
'null' is not assignable to File
How can I resolve this?
CodePudding user response:
Specify MyFile with nullable type.
interface ReportFiles {
ReportName: string;
Log: number;
MyFile?: File | null;
}
Sample Demo on Typescript Playground
CodePudding user response:
We usually don't assign type any because it defeats the purpose of type checking but any type may be used when type changes are expected.
export interface ReportFiles{
ReportName: string;
Log: number;
MyFile: any;
}
Seeing your need to initialize a obj RF type of ReportFiles I would suggest converting ReportFiles and File into class objects. But this is optional. If you need/wish to create an instance of perhaps a custom object, whilst getting the benefits of type-checking things such as arguments, return types or generics - a class makes sense. If you’re not creating instances - we have interfaces at our disposal, and their benefit comes from not generating any source code, yet allowing us to somewhat “virtually” type-check our code.
