我使用最新的3.8.4,但是我发现很多时候,引擎对于ts特性的支持还是太糟糕,无法正常使用的promise,鬼一般的原型链,不能自定义的装饰器(属性装饰器)。我不清楚那些说ts soha服务器和客户端的人现在有啥心得,我觉得都是坑。我使用的是nodejs16和 “typescript”: “^5.4.5”,将这些代码复制到cocos就不一定能跑起来了。这两个运行时其中的差距真的没救了
无法正常使用的promise,是怎么无法正常使用
不能使用 reject
不就是半残废吗?有其他平台的ts代码包含promise的都不能直接复制
我用 2.4.8 怎么都可以,3.x 退步了?
贴代码 为什么没办法使用
不太清楚了你说的reject了,我们都是正常使用的
promise 应该能用吧。你是怎样的写法。
装饰器 typescript 5.x 是 Stage 3 decorator implementation,cocos 的是 stage 2 decorators implementation, 应该是这个问题吧。
有问题建议上demo,上代码。
我代码里大量的promise,不知道是怎么出现的问题
贴代码吧。
promise的历史太久远了,这个装饰器的例子我有比较新的,一个属性装饰器,可以在nodejs上跑,正常使用get和set,但是在cocos的运行器内我就没法使用get和set
预格式化文本将缩进 4 格
// 定义装饰器工厂
export function PropDecorator() {
return function (target: any, propertyKey: string) {
let value = target[propertyKey];
const handler: PropertyDescriptor = {
get: function () {
console.log( Getting the value of ${propertyKey}: ${value});
return value;
},
set: function (newVal) {
console.log(Setting the value of ${propertyKey} from ${value} to ${newVal});
value = newVal;
},
enumerable: true,
configurable: true
};
delete target[propertyKey];
Object.defineProperty(target, propertyKey, handler);
};
}
class Entity {
@PropDecorator()
num: number = 10;
}
// 使用示例
const entity = new Entity();
console.log(entity.num); // 输出: Getting the value of num: 10
entity.num = 20; // 输出: Setting the value of num from 10 to 20
console.log(entity.num); // 输出: Getting the value of num: 20
另外还有个原型链的晚一点发给大家看看,谢谢大家这么帮忙
export abstract class BaseSkill{
id:number
preAttr:ISkillAttr
postAttr:ISkillAttr
mainAttr:IMainAttr
releaseDistance:number
targetType:targetTypeString
frameCount:number
skillAttackActionBase:(hero:BaseHero,mainAttr:IMainAttr,actionType: Action2Type,physicalAttack:number,
magicAttack:number,pureAttack:number,target:BaseHero,battleHeroes:IBattleHeros)=>Array<Object>
get skillAttackAction():(hero:BaseHero,mainAttr:IMainAttr,actionType: Action2Type,physicalAttack:number,
magicAttack:number,pureAttack:number,target:BaseHero,battleHeroes:IBattleHeros)=>Array<Object>{
if(this.skillAttackActionBase) return this.skillAttackActionBase
return Object.getPrototypeOf(this).skillAttackAction
}
skillDecreaseActionBase:(hero:BaseHero,mainAttr:IMainAttr,changeHp:number,changeEr:number,actionType: Action2Type,
target:BaseHero,battleHeroes:IBattleHeros)=>Array<Object>
get skillDecreaseAction():(hero:BaseHero,mainAttr:IMainAttr,changeHp:number,changeEr:number,actionType: Action2Type,
target:BaseHero,battleHeroes:IBattleHeros)=>Array<Object>{
if(this.skillDecreaseActionBase) return this.skillDecreaseActionBase
return Object.getPrototypeOf(this).skillDecreaseAction
}
constructor(id:number,pre_attr:ISkillAttr,post_attr:ISkillAttr,main_attr:IMainAttr,releaseDistance:number,targetType:targetTypeString){
this.id = id
this.releaseDistance = releaseDistance
this.targetType = targetType
this.preAttr = pre_attr
this.postAttr = post_attr
this.mainAttr = main_attr
}
get preFramCount():number{
return this.preAttr.frameCount
}
get postFramCount():number{
return this.postAttr.frameCount
}
get mainFramCount():number{
return this.mainAttr.frameCount
}
abstract actionPre(hero:BaseHero):Array<Object>
abstract actionMain(hero:BaseHero,actionType:Action2Type,battleHeroes:IBattleHeros):Array<Object>
abstract actionPost(hero:BaseHero):Array<Object>
}
我在nodejs 里是不需要写skillAttackActionBase 和 get skillAttackAction,直接定义skillAttackAction,所有继承BaseSkill的子类如果 skillAttackAction 是undefine就会进入父类的属性中,但是在cocos运行时里子类没有查找到skillAttackAction会直接返回undefine
promise reject 的时候给你报错弹框,这个体验肯定是不行的。
不过我在使用层面进行了规避,只用 resolve 不用 reject。通过不同返回值判断就好了。
这是因为也不支持Promise.catch吧 
这是因为也不支持Promise.catch吧

这个我在客户端从来不用,应为没出现过…
后端服务器有用到捕获异常,都是统一在底层以下面的模式出现
try {
let requestAsyncHandler = null;
await requestAsyncHandler();
}catch(err) {
context.response(err_code.xxxx, err_param)
}
跟编译器和支持的ts版本有关吧
之前看开发团队6月份说3.8用的babel编译器支持的ts是4.9.5
TypeScript 5.0 实现了新的装饰器标准
我在creator 3.8.4也用的typescript 4.9.5,不敢升级
升上去问题一大堆,又退回来了,暂时还用不到新特性
比较麻烦的是有些第3方库的 *.d.ts 用的typescript 5.x的语法
还要手动改
那感觉并行开发个支持ts5的creator 4.0 preview得了,短期内把creator3的功能逐步往过适配,中期内并发维护两者,长期把4.1定为下一个lts,鼓励开发者往新版本迁移。