Typescript 类型脚本推断不正确?(根据作为串联字符串的字段,从对象的并集推断一个类型的对象)

Typescript 类型脚本推断不正确?(根据作为串联字符串的字段,从对象的并集推断一个类型的对象),typescript,Typescript,在下面的代码段中,如何让typescript在if语句的范围内推断d包含a type Data<NS extends string> = { type: `${NS}/1`; a: string } | { type: `${NS}/2`; b: string }; const foo = <NS extends string>(name: NS, d: Data<NS>): void => { if (d.type === (`${na

在下面的代码段中,如何让typescript在if语句的范围内推断d包含a

  type Data<NS extends string> = { type: `${NS}/1`; a: string } | { type: `${NS}/2`; b: string };
  const foo = <NS extends string>(name: NS, d: Data<NS>): void => {
    if (d.type === (`${name}/1` as const)) {
      // Property 'a' does not exist on type 'Data<NS>'. Property 'a' does not        _exist on type '{ type: `${NS}/2`; b: string; }'.
      d.a;
    }
  };
type Data={type:`{NS}/1`;a:string}{type:`{NS}/2`;b:string};
常量foo=(名称:NS,d:Data):void=>{
if(d.type==(`${name}/1`as const)){
//属性“a”在类型“Data”上不存在。属性“a”在类型“{type:`NS}/2`;b:string;}”上不存在。
d、 a;
}
};
上面的代码段类似于下面的代码段,它确实可以正确推断,但不包含串联的字符串常量(这是我所需要的):

type Data={type:`1`;a:string}{type:`2`;b:string}
常量foo=(名称:NS,d:Data):void=>{
if(d.type==(`1`as const)){
d、 a;
} 
}
type Data<NS extends string> = { type: `1`; a: string } | { type: `2`; b: string }
const foo = <NS extends string>(name: NS, d: Data<NS>): void => { 
  if (d.type === (`1` as const)) {
    d.a;
  } 
}