typescript项目内用getComponent类型问题

比如有一个脚本挂在主场景上是这样::14::2:

const {ccclass, property} = cc._decorator;
@ccclass
export default class RadioButton extends cc.Component{
    target: string;:6:
    onLoad(){
        
    }
}

另一个脚本挂在主场景上是这样:

import RadioButton from './RadioButton';
type rb = RadioButton;

const {ccclass, property} = cc._decorator;
@ccclass
export default class RadioButton extends cc.Component{
    
    aaa: rb;
    onLoad(){
        this.aaa = this.node.getComponent('RadioButton');
       主要就在这里了提示类型错误,因为getComponent返回的是cc.Node类型,aaa是上衣个脚本的类型,所以提示cc.Node上没有target属性,但是getComponent(‘RadioButton’)按理说应该就是rb类型才对啊,请问怎么做类型转换,别说都转成any类型啊,那样智能提示都没有了
    }
}

getcomponent() as rb;

这就是ts的美妙~

帮看下

波浪线提示的错误信息是
[ts]
Argument of type ‘(value: Node, index: number) => any’ is not assignable to parameter of type ‘(value: Node, index: number, array: Node[]) => value is RadioButton & Node’.
Signature ‘(value: Node, index: number): any’ must be a type predicate.

你那尖括号是干什么用的?

改成
this.node.getComponent(RadioButton);

即可

1赞

但是在filter回调函数里面的getComponent(RadioButton)l类型还是不对

泛型推倒

filter<RadioButton>

写成filter还是有错误


错误信息是: [ts]
Type ‘RadioButton’ does not satisfy the constraint ‘Node’.
Property ‘groupIndex’ is missing in type ‘RadioButton’.

那你查一下 typescript 文档吧,可能要改成 cc.Node, RadioButton 之类的?