Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以为Typescript中的变量执行类型断言吗?该变量应该对整个块/函数有效?_Typescript_Casting - Fatal编程技术网

我可以为Typescript中的变量执行类型断言吗?该变量应该对整个块/函数有效?

我可以为Typescript中的变量执行类型断言吗?该变量应该对整个块/函数有效?,typescript,casting,Typescript,Casting,我有一个类型为null的状态变量|某些类型。但是有些函数期望它是some\u TYPE,而这些函数100%确定在运行时状态变量将是some\u TYPE 我是否可以键入一个对整个函数范围有效的强制转换/断言变量 例如: const [myState,setMyState] = useState<null | SOME_TYPE>(null); function doSomething() { // THIS FUNCTION NEEDS myState TO BE SOME_T

我有一个类型为
null的状态变量|某些类型
。但是有些函数期望它是
some\u TYPE
,而这些函数100%确定在运行时状态变量将是
some\u TYPE

我是否可以键入一个对整个函数范围有效的强制转换/断言变量

例如:

const [myState,setMyState] = useState<null | SOME_TYPE>(null);

function doSomething() {
  // THIS FUNCTION NEEDS myState TO BE SOME_TYPE
  // IT'S 100% GUARANTEED THAT WHEN IT RUNS, myState WILL BE SOME_TYPE

  // IT WILL ACCESS MULTIPLE PROPERTIES OF MY STATE
  console.log(myState.someProp as SOME_TYPE);
  console.log(myState.someOther as SOME_TYPE);
  console.log(myState.another as SOME_TYPE);
}

有语法或建议吗?

我认为新变量是可以接受的解决方案,但您的另一个选择是

与强制转换的新变量相反,将在运行时检查预编辑

function isSomeType(x: null | SOME_TYPE): x is SOME_TYPE {
  return x !== null;
} 

function doSomething() {
  if (isSomeType(myState)) {
    console.log(myState.stateSomeType);
    console.log(myState.someOther);
    console.log(myState.another);
  }
}
此外,由于您的类型是
SOME_type | null
,所以非null保护也可以工作:

function doSomething() {
  if (myState !== null) {
    console.log(myState.stateSomeType);
    console.log(myState.someOther);
    console.log(myState.another);
  }
}

您可以通过多种方式实现这一点

function doSomething() {
    if (!myState) return; // Will return if myState is null
   
    // myState is not null for the rest of the function
    // Since it could only be null or SOME_TYPE then it must be SOME_TIME
    console.log(myState.someProp);
    console.log(myState.someOther);
    console.log(myState.another);
}
另一种方法是创建另一个转换为某种类型的变量

function doSomething() {
    // This cast is basically telling the compiler "shut up, I know what I am doing"
    // It's up to you to ensure the correctness and make sure myState is SOME_TYPE
    const myVar = myState as SOME_TYPE;
   
    console.log(myVar.someProp);
    console.log(myVar.someOther);
    console.log(myVar.another);
}
您也可以使用类型保护。当您的类型是某个| type | null时,您只需要检查myState是否为truthy,或者如果它是falsy,则从函数返回,如上面的第一个代码段所示

如果你的状态是某种类型的|另一种类型的|。一张简单的支票是不够的。您可以使用类型保护

function isSomeType(val: SOME_TYPE|ANOTHER_TYPE): val is SOME_TYPE {
    // do a check here to ensure that val is SOME_TYPE
    // For example you can check for the presence of a property that is in SOME_TYPE and not ANOTHER_TYPE
    return typeof val === 'object' && typeof ((val as SOME_TYPE).someProp) === 'string';
}

function doSomething() {
    // Before the call myState is SOME_TYPE|ANOTHER_TYPE
    if (!isSomeType(myState)) return;
    // After call myState is SOME_TYPE

    console.log(myState.someProp);
    console.log(myState.someOther);
    console.log(myState.another);
}

在没有新变量的情况下,如果对象位于函数的同一块范围内,则可以更改函数的签名,如下所示:

const实例={
一些建议:5,
其他人:是的,
另一个:“测试”
};
doSomething(实例);
函数doSomething(arg1:{[key-in-keyof-typeof-instance]:SOME_-TYPE}){
//你的代码在这里
}

如果使用新变量,则可以强制转换为具有与对象相同键的类型:

const实例={
一些建议:5,
其他人:是的,
另一个:“测试”
};
接口类型{
//您的类型定义
};
const supportVariable=实例为{[key in keyof typeof instance]:SOME_TYPE};
现在每个属性都设置为某种类型

function doSomething() {
    // This cast is basically telling the compiler "shut up, I know what I am doing"
    // It's up to you to ensure the correctness and make sure myState is SOME_TYPE
    const myVar = myState as SOME_TYPE;
   
    console.log(myVar.someProp);
    console.log(myVar.someOther);
    console.log(myVar.another);
}
console.log(myState.someProp);//不需要用作某种类型的
console.log(myState.someOther);//不需要用作某种类型的
console.log(myState.other);//不需要用作某种类型的

刚刚注意到没有新的变量要求。用户定义的类型guard如何:
函数isSomeType(x:null | SOME_type):x是SOME_type{return x!==null;}
@Lesiak从未使用过。怎么样?@Lesiak,谢谢。我试试看。请随意写一个带有该建议的答案。