Javascript 安全浇铸混合型到合适的流动型

Javascript 安全浇铸混合型到合适的流动型,javascript,type-conversion,flowtype,Javascript,Type Conversion,Flowtype,假设返回了一些随机对象数据。现在我希望验证它并将其转换为appropiate类型,以便在我的应用程序的其余部分中使用。为此,我编写了以下utilty函数 static convertSingleSafe<T: (number | boolean | string | void)>( data: {[string]: mixed}, name: string, type: 'number' | 'boolean' | 'string' |

假设返回了一些随机对象数据。现在我希望验证它并将其转换为appropiate类型,以便在我的应用程序的其余部分中使用。为此,我编写了以下utilty函数

static convertSingleSafe<T: (number | boolean | string | void)>(
        data: {[string]: mixed},
        name: string,
        type: 'number' | 'boolean' | 'string' | 'object' | 'undefined'): T {
    if (typeof data[name] === type ) {
        return data[name]
    } else  {
        throw new KeyError(`Badly formated value ${name}`, '')
    }
}
错误:(191,41)无法调用
FlowTest.convertSingleSafe
,而
d
绑定到
data
,因为在索引器属性中:混合[1]与数字[2]不兼容。或混合[1]与布尔[3]不兼容。或混合[1]与字符串[4]不兼容


那么(如何)我可以做到这一点,而不必进行任何
强制转换?

在flow中,您必须了解的是,它不是很聪明。Flow基本上是在寻找一个类似于
typeof===“number”
的构造,比如想象它使用正则表达式来扫描代码(事实并非如此)。一个我认为你想要的例子:

static convertSingleSafe(
  data: {[string]: mixed},
  name: string,
  type: 'number' | 'boolean' | 'string' | 'object' | 'undefined'
): number | boolean | string | {} | void {
  const subject = data[name];
  if (type === 'number' && typeof subject === 'number') {
    return subject;
  }
  if (type === 'boolean' && typeof subject === 'boolean') {
    return subject;
  }
  if (type === 'string' && typeof subject === 'string') {
    return subject;
  }
  if (type === 'undefined' && typeof subject === 'undefined') {
    return subject;
  }
  if (type === 'object' && subject && typeof subject === 'object') {
    return subject;
  }
  throw new KeyError(`Badly formated value ${name}`, '');
}

而且。

在flow中,你必须了解的是它不是很聪明。Flow基本上是在寻找一个类似于
typeof===“number”
的构造,比如想象它使用正则表达式来扫描代码(事实并非如此)。一个我认为你想要的例子:

static convertSingleSafe(
  data: {[string]: mixed},
  name: string,
  type: 'number' | 'boolean' | 'string' | 'object' | 'undefined'
): number | boolean | string | {} | void {
  const subject = data[name];
  if (type === 'number' && typeof subject === 'number') {
    return subject;
  }
  if (type === 'boolean' && typeof subject === 'boolean') {
    return subject;
  }
  if (type === 'string' && typeof subject === 'string') {
    return subject;
  }
  if (type === 'undefined' && typeof subject === 'undefined') {
    return subject;
  }
  if (type === 'object' && subject && typeof subject === 'object') {
    return subject;
  }
  throw new KeyError(`Badly formated value ${name}`, '');
}

如果您试图分配给类型化变量,例如
const a:number=convertsinglesinglesafe({a:1},'a','number'),则。

不起作用向答案添加了通用版本,因为链接太长,无法添加注释。您可能需要这样的东西。如果尝试分配给类型化变量,例如
const a:number=convertSingleSafe({a:1},'a','number'),则不起作用向答案添加了通用版本,因为链接太长,无法添加注释。你会想要那样的。
static convertSingleSafe(
  data: {[string]: mixed},
  name: string,
  type: 'number' | 'boolean' | 'string' | 'object' | 'undefined'
): number | boolean | string | {} | void {
  const subject = data[name];
  if (type === 'number' && typeof subject === 'number') {
    return subject;
  }
  if (type === 'boolean' && typeof subject === 'boolean') {
    return subject;
  }
  if (type === 'string' && typeof subject === 'string') {
    return subject;
  }
  if (type === 'undefined' && typeof subject === 'undefined') {
    return subject;
  }
  if (type === 'object' && subject && typeof subject === 'object') {
    return subject;
  }
  throw new KeyError(`Badly formated value ${name}`, '');
}