typescript 从具有 stringInterpolation 的 stringObject 获取值并填充插值;

typescript get value from stringObject that has stringInterpolation and fill the interpolation;

示例:

const objectA = {
  a: `a ${fruit} is red`,
  b: 42,
  c: false
};

const fruit = 'strawberry';

console.log(objectA['a']);

控制台:

Error: fruit is not defined

我想知道这是否可能。

如果是我怎么办

您可以这样做,但必须按正确的顺序进行:

const fruit = 'strawberry';

const objectA = {
  a: `a ${fruit} is red`,
  b: 42,
  c: false
};

console.log(objectA['a']);