English 中文(简体)
• 如何区分与文字类型有关的两种变数?
原标题:How to type two variables that are correlated in TypeScript?
  • 时间:2024-03-07 01:01:37
  •  标签:
  • typescript

我有以下结构:<条码>ComboChartSpec> 和<条码>。 I m 界定处理<代码>的实用功能 ComboChartSpec。

如果我这样界定,它就会发挥作用。

function process1() {
  const yAxisType =  primary ;
  const otherYAxisType =  secondary ;
  return {
    [yAxisType]: foo,
    [otherYAxisType]: bar
  }
}
function process2() {
  const yAxisType =  secondary ;
  const otherYAxisType =  primary ;
  return {
    [yAxisType]: foo,
    [otherYAxisType]: bar
  }
}

我想合并这两个职能。 但是,我留下了TS错误。 我如何解决这一问题? 感谢!

function process(yAxisType:  primary  |  secondary ) {
  const otherYAxisType = yAxisType ===  primary  ?  secondary  :  primary ;
  return {
    [yAxisType]: foo,
    [otherYAxisType]: bar
  }
}
Type  { [x: string]: { fields: never[]; } | { type: "quantitative"; }; scale: { type: "quantitative"; }; }  is not assignable to type  ComboChartAxisEncoding<"editor"> .
  Type  { [x: string]: { fields: never[]; } | { type: "quantitative"; }; scale: { type: "quantitative"; }; }  is not assignable to type  ComboChartSingleAxisEncoding<"editor"> .
    Type  { [x: string]: { fields: never[]; } | { type: "quantitative"; }; scale: { type: "quantitative"; }; }  is missing the following properties from type  { primary: ComboChartSingleAxisSectionEncoding<"editor">; secondary: ComboChartSingleAxisSectionEncoding<"editor">; scale: QuantitativeScale; } : primary, secondaryts(2322)
index.ts(85, 3): The expected type comes from property  y  which is declared here on type  ComboChartEncodingMap<"editor"> 
问题回答




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签