TS4060:导出函数的返回类型已经或正在使用私有名称“类”

Posted

技术标签:

【中文标题】TS4060:导出函数的返回类型已经或正在使用私有名称“类”【英文标题】:TS4060: Return type of exported function has or is using private name 'class' 【发布时间】:2019-08-08 10:01:15 【问题描述】:

我被困在这里了。 Typescript 一直在抱怨:

TS4060:导出函数的返回类型有或正在使用私有名称 “班级”学生

test.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 上复制粘贴完全相同的代码,但我没有收到任何错误,并且它编译得很好。

复制:

usage.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())

【问题讨论】:

听起来像是返回类型中的范围可见性问题。 你找到解决办法了吗? 也许你应该看看here。 this你要分享的ts playground链接? 【参考方案1】:

您应该将私有成员设为公开。没有办法解决这个问题。

如果您使用来自另一个模块的非导出类型,则会引发类似情况。

【讨论】:

以上是关于TS4060:导出函数的返回类型已经或正在使用私有名称“类”的主要内容,如果未能解决你的问题,请参考以下文章