Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cc.d.ts 错误: ITweenOption 中的 target?: object 应该为 target?: any #16937

Closed
finscn opened this issue Apr 28, 2024 · 24 comments
Closed
Assignees
Labels
Bug P1 Must be finished in the milestone
Milestone

Comments

@finscn
Copy link
Contributor

finscn commented Apr 28, 2024

Cocos Creator version

3.8.2

System information

all

Issue description

用 object 在很多时候会报错.

Relevant error log output

No response

Steps to reproduce

Minimal reproduction project

No response

@finscn finscn added Bug Needs Triage Needs to be assigned by the team labels Apr 28, 2024
@dumganhar
Copy link
Contributor

很多时候 能否举例说明一下什么情况下出了什么问题?

@finscn
Copy link
Contributor Author

finscn commented Apr 29, 2024

很多时候 能否举例说明一下什么情况下出了什么问题?

这不是明摆着的吗?
object 并不能兼容其他类型啊.

image

@finscn
Copy link
Contributor Author

finscn commented Apr 29, 2024

如果要让 current 在函数里能够准确的推测出类型 , 我需要给 current 指定类型. 但是无论什么类型, 都会出现类似错误.
只能强制 any. 但是用了any , 在函数里面 current无法提示任何属性.

@finscn
Copy link
Contributor Author

finscn commented Apr 29, 2024

image

@finscn
Copy link
Contributor Author

finscn commented Apr 29, 2024

有时候我对cocos真的是恨铁不成钢. . cocos是不是雇佣了一群根本不会ts的人在写ts啊?

@dumganhar
Copy link
Contributor

大佬批评的是,还请不吝赐教。

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 哪个配置导致的?

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

@dumganhar 这事不归eslint管啊。是ts编译器给出的错误。
另外别提你们的eslint了,你们自己多少代码无法通过你们自己的eslint ,心里没数吗。
唉。看到cocos现在这样,我是真的痛心疾首啊。
cc 3 这ts用的真糟心

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

要不给 option也加个t吧。
mmexport1714448455577

反正这里不能用object。这是明摆着的。object在ts里指代的是 {},并不是任意对象。
tween不仅仅支持{},还支持其他很多类型,那option里的target为啥要用object?你跟我解释解释?

这事无关eslint 无关编辑器报错。这事是ts常识。

@dumganhar
Copy link
Contributor

难道 cc.Node 不是 object 子类型吗?我在 vscode 中写这些代码,并没有你说的这个报错。所以你说有问题,就甩一句话出来,这是解决问题的方式吗?

这里 target: object 类型表示需要其类型是对象类型。你改成 target: any,你是希望传递 number?boolean?还是 null , undefined 作为 target ?

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

难道 cc.Node 不是 object 子类型吗?我在 vscode 中写这些代码,并没有你说的这个报错。所以你说有问题,就甩一句话出来,这是解决问题的方式吗?

这里 target: object 类型表示需要其类型是对象类型。你改成 target: any,你是希望传递 number?boolean?还是 null , undefined 作为 target ?

当然不是了。js里是,ts里object是 {} 。
ts的 类型 object
和 typeof foo == "object"里的 object不是一个概念

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

这里的 object 和 ts 的 typeof {} 是等价的

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

并不是说object 只能表示 {}

@dumganhar
Copy link
Contributor

请指出我上述的测试代码,为什么没有你说的报错。所以我让你别一句话吐槽,show me the code,ok?

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

typeof 在ts里也有两种意义。
在表达式里,和js一样,结果是一个字符串。
用于类型声明时,结果是一个ts的类型

@yanjifa
Copy link
Contributor

yanjifa commented Apr 30, 2024

两位,我实测了一下,开了严格模式会报错, 没开严格模式不会报错

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

我们先明确一个事情,
我这里说的都是 类型兼容的问题,不是实例兼容的问题。
这个先对齐下。否则就是鸡同鸭语了。

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

我现在为了 在onUpdate李能够获得current具体的类型信息,我需要在current后面补充声明具体的类型,比如cc.Node 。
这个时候 ts就会判定 cc.Node类型和object类型不兼容。
但是current这个实例本身是兼容object类型的。

这里有点绕。
你写个具体的tween代码,然后在current后面声明个具体类型 你就明白了

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

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

image

以上是我这边的错

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

为了正确去到 position , 正确的方式是 在current后面补充具体的类型声明.

image

但是这时候 onUpdate就会报错. 因为 cc.Node 和 object 类型不兼容.

@yanjifa
Copy link
Contributor

yanjifa commented Apr 30, 2024

开启严格模式
screenshot-20240430-141518
关闭严格模式
screenshot-20240430-141549
@finscn 这是我这边测试的,不知道你那是不是开了严格模式的原因

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

开启严格模式 screenshot-20240430-141518 关闭严格模式 screenshot-20240430-141549 @finscn 这是我这边测试的,不知道你那是不是开了严格模式的原因

首先 我确实开启了严格模式. 其次 难道你们不是也应该开启才对吗?

image

你们自己开发的时候 偷偷把严格模式关了 是不是因为严格模式很烦?
其实严格模式需要精细化配置. 默认全开 和 全关 都是不对的.

我之前给你们提过建议, 建议这样设置:
image

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

关于 tsconfig 和 eslint 的设置问题 , 很久之前我和你们的员工深入交流过 (微信上) . 可惜后来他离职了. 我也是在没有耐心把说过的话再重复一遍了. 以后有机会去厦门当面交流吧.

反正现在 cocos 的tsconfig 设置挺坑的. 你们自己开发的时候应该也觉得烦.
尤其是 "strictNullChecks" 和 "strictPropertyInitialization" 为true时.

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

说回这个issue本身, 难道官方的 d.ts 不是应该做到 严格模式 和 非严格模式都不应该出错才对吗?
除非你们官方要求 使用cocos的用户 不能开启严格模式.

@finscn
Copy link
Contributor Author

finscn commented Apr 30, 2024

关于 tsconfig , 我顺便回复了一下 这个 #16542 里面 @dumganhar 的问题.

@dumganhar dumganhar added P1 Must be finished in the milestone and removed Needs Triage Needs to be assigned by the team labels May 23, 2024
@dumganhar dumganhar added this to the 3.8.5 milestone May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug P1 Must be finished in the milestone
Projects
Status: Done
Development

No branches or pull requests

3 participants