Typescript 类型C运算符中使用的RTIP字符串文本类型

Typescript 类型C运算符中使用的RTIP字符串文本类型,typescript,Typescript,你好,Stack社区, 假设已定义以下字符串文字: type FormConstants = 'new' | 'edit'; 现在我想检查一下,valueval是否等于FormConstants定义的任何值: function inSet(val: string): boolean { if ((val as FormConstants) === XXX ) { return true; } else { return false

你好,Stack社区,
假设已定义以下字符串文字:

type FormConstants = 'new' | 'edit';
现在我想检查一下,value
val
是否等于
FormConstants
定义的任何值:

    function inSet(val: string): boolean {
      if ((val as FormConstants) === XXX ) {
        return true;
      } else {
        return false;
      }
    }
然而,
XXX
是我想要的陈述。解决方案如下:

  • keyof FormConstants
  • 格式常量
  • typeof FormConstants
失败得很惨,因为它们都使用
FormConstants
作为值,而
FormConstants
表示一种类型

编辑:
如果elseif else聚合的surestatements

类型在运行时不存在,那么我想肯定地避免使用
开关
/
,因此来自它们的任何信息只能指导编译时检查,而不是运行时行为。您可以选择以下几种选项之一:

使用字符串枚举,它既是一种类型,也是一种运行时对象:

enum FormConstants { new = 'new', edit = 'edit' }

function inSet(val: string): boolean {
    if (Object.values(FormConstants).includes(val)) {
    return true;
    } else {
    return false;
    }
}
或者使用字符串文字数组来构造值数组,并派生
FormConstants
形式:

function stringLilteralArray<T extends string>(values: T[]) : T[] {
    return values
}
const FormConstants = stringLilteralArray(['new', 'edit']) ;
type FormConstants = typeof FormConstants[number];

function inSet(val: string): boolean {
    if (FormConstants.includes(val as FormConstants)) {
    return true;
    } else {
    return false;
    }
}
函数stringLilteralArray(值:T[]):T[]{
返回值
}
const FormConstants=stringlilteralray(['new','edit']);
类型FormConstants=类型FormConstants[编号];
函数插入(val:string):布尔值{
if(FormConstants.includes(val作为FormConstants)){
返回true;
}否则{
返回false;
}
}
如果字符串文本的并集不在您的控制之下,您可以构造一个函数来构建所有值的数组,以确保传递的值必须与并集中的值完全一致(编译器将强制执行此操作)。因此,即使您必须复制这些值,它们也不可能偏离:

type FormConstants = 'new' | 'edit';
function unionValues<T extends string>(values: { [P in T]: P}) : T[] {
    return Object.values(values);
}
const FormConstants = unionValues<FormConstants>({ new : 'new', edit :'edit' }) ;

function inSet(val: string): boolean {
    if (FormConstants.includes(val as FormConstants)) {
    return true;
    } else {
    return false;
    }
}
type FormConstants='new'|'edit';
函数值(值:{[P in T]:P}):T[]{
返回Object.values(值);
}
const FormConstants=unionValues({new:'new',edit:'edit'});
函数插入(val:string):布尔值{
if(FormConstants.includes(val作为FormConstants)){
返回true;
}否则{
返回false;
}
}
编辑

上述方法的一个变体,不使用数组,使用对象键,肯定会更快:

type FormConstants = 'new' | 'edit';
function unionValues<T extends string>(values: { [P in T]: true }) : typeof values {
    return values;
}
const FormConstants = unionValues<FormConstants>({ new : true, edit :true }) ;

function inSet(val: string): boolean {
    if (val in FormConstants) {
        return true;
    } else {
         return false;
    }
} 
type FormConstants='new'|'edit';
函数unionValues(值:{[P in T]:true}):值的类型{
返回值;
}
const FormConstants=unionValues({new:true,edit:true});
函数插入(val:string):布尔值{
if(形式常量中的val){
返回true;
}否则{
返回false;
}
} 

类型在运行时不存在,因此来自它们的任何信息只能指导编译时检查,而不能指导运行时行为。您可以选择以下几种选项之一:

使用字符串枚举,它既是一种类型,也是一种运行时对象:

enum FormConstants { new = 'new', edit = 'edit' }

function inSet(val: string): boolean {
    if (Object.values(FormConstants).includes(val)) {
    return true;
    } else {
    return false;
    }
}
或者使用字符串文字数组来构造值数组,并派生
FormConstants
形式:

function stringLilteralArray<T extends string>(values: T[]) : T[] {
    return values
}
const FormConstants = stringLilteralArray(['new', 'edit']) ;
type FormConstants = typeof FormConstants[number];

function inSet(val: string): boolean {
    if (FormConstants.includes(val as FormConstants)) {
    return true;
    } else {
    return false;
    }
}
函数stringLilteralArray(值:T[]):T[]{
返回值
}
const FormConstants=stringlilteralray(['new','edit']);
类型FormConstants=类型FormConstants[编号];
函数插入(val:string):布尔值{
if(FormConstants.includes(val作为FormConstants)){
返回true;
}否则{
返回false;
}
}
如果字符串文本的并集不在您的控制之下,您可以构造一个函数来构建所有值的数组,以确保传递的值必须与并集中的值完全一致(编译器将强制执行此操作)。因此,即使您必须复制这些值,它们也不可能偏离:

type FormConstants = 'new' | 'edit';
function unionValues<T extends string>(values: { [P in T]: P}) : T[] {
    return Object.values(values);
}
const FormConstants = unionValues<FormConstants>({ new : 'new', edit :'edit' }) ;

function inSet(val: string): boolean {
    if (FormConstants.includes(val as FormConstants)) {
    return true;
    } else {
    return false;
    }
}
type FormConstants='new'|'edit';
函数值(值:{[P in T]:P}):T[]{
返回Object.values(值);
}
const FormConstants=unionValues({new:'new',edit:'edit'});
函数插入(val:string):布尔值{
if(FormConstants.includes(val作为FormConstants)){
返回true;
}否则{
返回false;
}
}
编辑

上述方法的一个变体,不使用数组,使用对象键,肯定会更快:

type FormConstants = 'new' | 'edit';
function unionValues<T extends string>(values: { [P in T]: true }) : typeof values {
    return values;
}
const FormConstants = unionValues<FormConstants>({ new : true, edit :true }) ;

function inSet(val: string): boolean {
    if (val in FormConstants) {
        return true;
    } else {
         return false;
    }
} 
type FormConstants='new'|'edit';
函数unionValues(值:{[P in T]:true}):值的类型{
返回值;
}
const FormConstants=unionValues({new:true,edit:true});
函数插入(val:string):布尔值{
if(形式常量中的val){
返回true;
}否则{
返回false;
}
} 

@Titan,非常感谢您一如既往。我知道字符串文本在运行时和接口一样不可用,不能以对象方式使用。您对打字稿的了解远远超出了专家水平。@Titan,非常感谢您一如既往。我知道字符串文本在运行时和接口一样不可用,不能以对象方式使用。你对打字稿的了解远远超出了专家的水平。