简体   繁体   English

参数中的Typescript类型断言

[英]Typescript type assertions in parameters

I'm liking typescript so far, but find that i need to do type assertion a lot. 到目前为止我喜欢打字稿,但发现我需要做很多类型的断言。 For example casting an EventTarget to an HTMLAnchorElement is a common use case for me. 例如,将一个EventTarget转换为HTMLAnchorElement是我常见的用例。 However to get that, i need to use something like the following: 但是为了达到这个目的,我需要使用以下内容:

getTabID(eventTarget: EventTarget) : string {
    // without the following variable, the compiler tells me that .hash 
    // is not a property of EventTarget, which according to the interface, it isn't.
    // So thats fine, i'll cast it to an Element
    let mEventTarget: HTMLAnchorElement = <HTMLAnchorElement>eventTarget
    let mTabID: string
    if(mEventTarget.hash){
        mTabID = mEventTarget.hash.split('#')[1]
    } 
    return mTabID
}

However this means that if I don't want the compiler to throw errors I need to create variables in my functions JUST to do type assertions. 但是这意味着如果我不希望编译器抛出错误,我需要在我的函数中创建变量JUST来做类型断言。 I don't mind the extra typing, but these end up in the JS as well and ends up wasting bytes in my js files. 我不介意额外的输入,但这些最终也在JS中,并最终在我的js文件中浪费字节。

I would like to be able to the following: 我希望能够做到以下几点:

getTabID(eventTarget: EventTarget) : string {
    let mTabID: string
    // Do the type assertion in the parameter 
    if(<HTMLAnchorElement> eventTarget.hash){
        mTabID = mEventTarget.hash.split('#')[1]
    } else {
        mTabID = mEventTarget.dataset.tabId
    }
    return mTabID
}

I've had a good look in the docs and SO and can't seem to find any way to do this. 我已经很好地了解了文档和SO,似乎无法找到任何方法来做到这一点。 Anyone have any ideas? 有人有想法么?

You can perform inline type assertions, by surrounding the assertion with parantheses: 您可以通过用parantheses包围断言来执行内联类型断言:

if((<HTMLAnchorElement>eventTarget).hash) {

You might also see what you can do to prevent the need for a type assertion, for example: 您可能还会看到可以做些什么来防止需要类型断言,例如:

getTabID(eventTarget: HTMLAnchorElement) : string {
    let mTabID: string;

    if(eventTarget.hash){
        mTabID = eventTarget.hash.split('#')[1]
    } 

    return mTabID
}

And lastly, watch out for the casing as you mixed EventTarget and eventTarget in one of your examples. 最后,在您的一个示例中混合使用EventTargeteventTarget ,请注意外壳。

You can use type guards to implement this. 您可以使用类型保护来实现此功能。

An example would be: 一个例子是:

function isAnchor(eventTarget: EventTarget): eventTarget is HTMLAnchorElement {
  return (<HTMLAnchorElement>eventTarget).hash != undefined;
}


function getTabID(eventTarget: EventTarget): string {
  let mTabID: string
  // Do the type assertion in the parameter 
  if (isAnchor(eventTarget)) {
    mTabID = eventTarget.hash.split('#')[1]
  } else {
    mTabID = eventTarget.dataset.tabId
  }
  return mTabID
}

Note: I didn't know what interface dataset was part of but you can make a type guard for it as well. 注意:我不知道接口数据集是什么的,但你也可以为它做一个类型保护。

You can learn more about type guards in the handbook here . 您可以在此处的手册中了解有关护卫的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM