我被困在这里。 Typescript 一直在提示:
TS4060: Return type of exported function has or is using private name 'class' Student
测试.ts
export default function MODULETOEXPORT(GreetingIntroTxt:string) {
class Student {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `"${GreetingIntroTxt}, " + this.greeting`;
}
}
class Teacher {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `"${GreetingIntroTxt}, " + this.greeting`;
}
}
class Professor {
name: string;
constructor(name: string) {
this.name = name;
}
greet() {
return `"${GreetingIntroTxt}, " + this.greeting`;
}
}
return {Professor, Student, Teacher}
}
虽然我在 Typescript Playground 上复制粘贴完全相同的代码,我没有收到任何错误,并且可以完美编译。
重现:
用法.ts
console.log('hello world app')
import module from './test';
const moduleES = module('Holla')
const moduleFR = module('Salut')
const moduleEN = module('Hello')
const greeterESStudent = new moduleES.Student("world");
console.log(greeterESStudent.greet())
const greeterFRStudent = new moduleES.Student("world");
console.log(greeterFRStudent.greet())
const greeterESTeacher= new moduleFR.Teacher("world");
console.log(greeterESTeacher.greet())
const greeterFRTeacher= new moduleFR.Student("world");
console.log(greeterFRTeacher.greet())
最佳答案
您应该将私有(private)成员设为公开。没有办法解决这个问题。
如果您使用来自另一个模块的非导出类型,则会引发类似情况。
关于javascript - TS4060 : Return type of exported function has or is using private name 'class' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214523/