Comments (24)
很多时候
能否举例说明一下什么情况下出了什么问题?
from cocos-engine.
很多时候
能否举例说明一下什么情况下出了什么问题?
这不是明摆着的吗?
object 并不能兼容其他类型啊.
from cocos-engine.
如果要让 current 在函数里能够准确的推测出类型 , 我需要给 current 指定类型. 但是无论什么类型, 都会出现类似错误.
只能强制 any. 但是用了any , 在函数里面 current无法提示任何属性.
from cocos-engine.
from cocos-engine.
有时候我对cocos真的是恨铁不成钢. . cocos是不是雇佣了一群根本不会ts的人在写ts啊?
from cocos-engine.
大佬批评的是,还请不吝赐教。
import { _decorator, Component, Node, Tween, tween, Vec3 } from 'cc';
import * as cc from 'cc';
const { ccclass, property } = _decorator;
class MyCustomData {
constructor (a) {
this._aaa = a;
}
_aaa = 0;
}
class TestOne {
_customData = new MyCustomData(0);
foo (): void {
const tweenDuration: number = 1.0;
tween(this._customData).to(
tweenDuration,
new MyCustomData(100),
this,
).start();
}
onUpdate (target: typeof this._customData, ratio: number): void {
console.log(`ratio: ${ratio}, aaa: ${target._aaa}`);
}
}
class TestTwo {
_node1 = new Node('node1');
_node2 = new Node('node2');
foo (): void {
this._node2.setPosition(100, 0, 0);
const tweenDuration: number = 1.0;
tween(this._node1).to(
tweenDuration,
this._node2,
this,
).start();
}
onUpdate (target: cc.Node, ratio: number): void {
console.log(`ratio: ${ratio}, ${target.name}: ${target.position.x}`);
}
}
@ccclass('MoveSprite')
export class MoveSprite extends Component {
start (): void {
new TestOne().foo();
new TestTwo().foo();
}
}
➜ Empty3D383 git:(main) ✗ npx eslint assets/MoveSprite.ts
/Users/james/tests/ccc/Empty3D383/assets/MoveSprite.ts
26:9 warning Unexpected console statement no-console
45:9 warning Unexpected console statement no-console
✖ 2 problems (0 errors, 2 warnings)
我按引擎的 eslint 配置,模拟你的写法,测试了一下。发现并没有 eslint 的报错。
你那个报错,是否是因为 eslint 哪个配置导致的?
from cocos-engine.
@dumganhar 这事不归eslint管啊。是ts编译器给出的错误。
另外别提你们的eslint了,你们自己多少代码无法通过你们自己的eslint ,心里没数吗。
唉。看到cocos现在这样,我是真的痛心疾首啊。
cc 3 这ts用的真糟心
from cocos-engine.
反正这里不能用object。这是明摆着的。object在ts里指代的是 {},并不是任意对象。
tween不仅仅支持{},还支持其他很多类型,那option里的target为啥要用object?你跟我解释解释?
这事无关eslint 无关编辑器报错。这事是ts常识。
from cocos-engine.
难道 cc.Node 不是 object 子类型吗?我在 vscode 中写这些代码,并没有你说的这个报错。所以你说有问题,就甩一句话出来,这是解决问题的方式吗?
这里 target: object 类型表示需要其类型是对象类型。你改成 target: any,你是希望传递 number?boolean?还是 null , undefined 作为 target ?
from cocos-engine.
难道 cc.Node 不是 object 子类型吗?我在 vscode 中写这些代码,并没有你说的这个报错。所以你说有问题,就甩一句话出来,这是解决问题的方式吗?
这里 target: object 类型表示需要其类型是对象类型。你改成 target: any,你是希望传递 number?boolean?还是 null , undefined 作为 target ?
当然不是了。js里是,ts里object是 {} 。
ts的 类型 object
和 typeof foo == "object"里的 object不是一个概念
from cocos-engine.
这里的 object 和 ts 的 typeof {} 是等价的
from cocos-engine.
并不是说object 只能表示 {}
from cocos-engine.
请指出我上述的测试代码,为什么没有你说的报错。所以我让你别一句话吐槽,show me the code,ok?
from cocos-engine.
typeof 在ts里也有两种意义。
在表达式里,和js一样,结果是一个字符串。
用于类型声明时,结果是一个ts的类型
from cocos-engine.
两位,我实测了一下,开了严格模式会报错, 没开严格模式不会报错
from cocos-engine.
我们先明确一个事情,
我这里说的都是 类型兼容的问题,不是实例兼容的问题。
这个先对齐下。否则就是鸡同鸭语了。
from cocos-engine.
我现在为了 在onUpdate李能够获得current具体的类型信息,我需要在current后面补充声明具体的类型,比如cc.Node 。
这个时候 ts就会判定 cc.Node类型和object类型不兼容。
但是current这个实例本身是兼容object类型的。
这里有点绕。
你写个具体的tween代码,然后在current后面声明个具体类型 你就明白了
from cocos-engine.
import * as cc from 'cc';
function test (): void {
const node = new cc.Node();
cc.tween(node)
.to(0.1, { position: cc.v3(0, 0, 0) },
{
onUpdate: (current, ratio: number) => {
console.log(current.position);
},
}
);
}
以上代码 你那边不报错吗? @dumganhar
以上是我这边的错
from cocos-engine.
为了正确去到 position , 正确的方式是 在current后面补充具体的类型声明.
但是这时候 onUpdate就会报错. 因为 cc.Node 和 object 类型不兼容.
from cocos-engine.
开启严格模式
关闭严格模式
@finscn 这是我这边测试的,不知道你那是不是开了严格模式的原因
from cocos-engine.
开启严格模式 关闭严格模式 @finscn 这是我这边测试的,不知道你那是不是开了严格模式的原因
首先 我确实开启了严格模式. 其次 难道你们不是也应该开启才对吗?
你们自己开发的时候 偷偷把严格模式关了 是不是因为严格模式很烦?
其实严格模式需要精细化配置. 默认全开 和 全关 都是不对的.
from cocos-engine.
关于 tsconfig 和 eslint 的设置问题 , 很久之前我和你们的员工深入交流过 (微信上) . 可惜后来他离职了. 我也是在没有耐心把说过的话再重复一遍了. 以后有机会去厦门当面交流吧.
反正现在 cocos 的tsconfig 设置挺坑的. 你们自己开发的时候应该也觉得烦.
尤其是 "strictNullChecks" 和 "strictPropertyInitialization" 为true时.
from cocos-engine.
说回这个issue本身, 难道官方的 d.ts 不是应该做到 严格模式 和 非严格模式都不应该出错才对吗?
除非你们官方要求 使用cocos的用户 不能开启严格模式.
from cocos-engine.
关于 tsconfig , 我顺便回复了一下 这个 #16542 里面 @dumganhar 的问题.
from cocos-engine.
Related Issues (20)
- Improve array property view HOT 5
- 编辑器中 双击 ts文件, 无法打开, 控制台报错 HOT 8
- 主画布无法实现透明背景.
- Spine组件 没有移除事件的方法
- Button could not be clicked after an exception is thrown while dispatching mouse/touch event.
- Mark component dirty in editor HOT 3
- Support `reverse` functionality for Tween
- Chrome Debug Task cannot debug engine code HOT 7
- spine隐藏时调用setAnimation报错 HOT 2
- The same 3D scene 3.8.3 takes up 90MB more memory than Unity 2021 HOT 5
- taobao Platform can not trigger clearLRU
- 当一个bundle中包含 "排除" 设置时, 不管这个bundle内容是否改变. 每次build后, 都会生成新的config.json HOT 2
- 希望能支持 新版本 (24,25,26)的 Android NDK HOT 2
- 编辑器中, 无法定位 预制体上 龙鼓组件的 json 文件
- 单选框添加必须选中一个选项的功能
- 资源在不同AssetBundle优先级中归属问题
- shader能内置个世界矩阵的逆矩阵吗? 翻了文档和源码都没有找到
- [native] Keep the same behavior of Promise rejection and multiple resolve as nodejs and web
- [bug] The generated JS code format was changed in v3.8.3
- Editor tool for collect shader variants information in the whole project, and runtime preload api for collected variants.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cocos-engine.