Typescript 带变量原因的模板字符串文字:s类型的参数不可分配给类型的参数

Typescript 带变量原因的模板字符串文字:s类型的参数不可分配给类型的参数,typescript,Typescript,这是一个连续的问题,这个问题: 类型数据={ 阵列对象:{ 名字:字符串; lastName:string; }[]; }; 键入ArrayElementType有一个小的StringNumberhack 解决方案: const索引='0'; const lit=`arrayObject.${index}.lastName`; //常量:字符串 const litAsConst=`arrayObject.${index}.lastName`as const; 命名空间源{ 类型数据={ 阵列对象

这是一个连续的问题,这个问题:

类型数据={
阵列对象:{
名字:字符串;
lastName:string;
}[];
};

键入ArrayElementType

有一个小的
StringNumber
hack

解决方案:

const索引='0';
const lit=`arrayObject.${index}.lastName`;
//常量:字符串
const litAsConst=`arrayObject.${index}.lastName`as const;
命名空间源{
类型数据={
阵列对象:{
名字:字符串;
lastName:string;
}[];
名字:字符串;
};
类型ArrayElementType=A扩展为只读(推断T)[]?T:从不
TupleOf型=
R['length']扩展了A?R:TupleOf;
类型整数=TupleOf;
类型索引=整数[数字];
类型PathFinder=键扩展字符串
?T[Key]扩展(字符串|布尔|数字|符号)[]
?`${Key}.${index}`
:T[Key]扩展对象[]
?`${Key}.${index}.${PathFinder}`
:T[键]扩展记录
?`${Key}.${PathFinder}`
:键
:从不;
功能寄存器(名称:PathFinder){
console.log(名称)
}

输入StringNumber=`${T}`/@Bill你觉得我的答案有用吗?
type Data = {
  arrayObject: {
    firstName: string;
    lastName: string;
  }[];
};

type ArrayElementType<A> = A extends readonly (infer T)[] ? T : never

type TupleOf<A extends number, R extends number[] = []> =
  R['length'] extends A ? R : TupleOf<A, [...R, R['length']]>;

type Integers = TupleOf<1>;

type Indexes = Integers[number];

type PathFinder<T, Key extends keyof T = keyof T> = Key extends string
    ? T[Key] extends (string | boolean | number | symbol)[]
      ? `${Key}.${Indexes}`
      : T[Key] extends object[]
        ? `${Key}.${Indexes}.${PathFinder<ArrayElementType<T[Key]>>}`
        : T[Key] extends Record<string, any>
          ? `${Key}.${PathFinder<T[Key]>}`
          : Key
    : never;

function register<Data>(name: PathFinder<Data>) {
  console.log(name)
}

const index = '0'

register(`arrayObject.${index}.lastName` as const); // works for TS 4.1
register(`arrayObject.${index}.lastName`); // works for TS 4.2

const i:number = '0'
register(`arrayObject.${index}.lastName` as const); fail
register(`arrayObject.${index}.lastName`); // fail