在一个文件中,我有我的班级实现..
// person.js
class Person {
...
}
在另一个文件中,我想使用此类型...
// job.js
class Job {
person: Person; // ERROR: Flow: identifier `Person`. Could not resolve name
/* @flow */
// person.js
type PersonOptions = {
status: string
}
class Person {
name: string;
options: PersonOptions;
constructor (name: string, optionalOptions: PersonOptions) {
this.name = name;
this.options = optionalOptions;
}
getStatus() {
console.log(`${this.name} is ${this.options.status}`);
}
}
// IN ANOTHER FILE
// job.js
class Job {
title: string;
person: Person; // ERROR: Flow: identifier `Person`. Could not resolve name
constructor (title: string, person: Person) {
this.title = title;
this.person = person;
}
getStatus() {
console.log(`${this.person.name} work with ${this.title}`);
}
}
答案 0 :(得分:2)
// in person.js
export default class Person {
//...
}
//in other file
import Person from './person.js';
class job {
person: new Person()
}
答案 1 :(得分:1)
在流程中,您可以导出类型
export type Person = {
// ...
}
并将其导入另一个文件
import type { Person } from './Person'
如果您的班级有默认导出,您可以简单地:
// Person.js
export default class Person {
//...
}
// another file
import type Person from './Person'
class Job {
title: string;
person: Person; // Flow should not throw an error
constructor (title: string, person: Person) {
this.title = title;
this.person = person;
}
getStatus() {
console.log(`${this.person.name} work with ${this.title}`);
}
}