Typescript-克隆一个对象并强制转换为另一个类型

Typescript-克隆一个对象并强制转换为另一个类型,typescript,Typescript,我正在尝试克隆一个非类型化对象。我想在克隆时播放它。 下面是一个非常简单的代码: const typedOject: type123 = {...untypedObject} as type123; typeObject.a = 1; export interface type123 { a: number; b: number; } 但编译器抱怨: 类型“any[]”上不存在属性“a” 那么,为什么新对象是any类型呢 感谢这些类型的转换在处理传入的API数据时很常见 以下是我

我正在尝试克隆一个非类型化对象。我想在克隆时播放它。 下面是一个非常简单的代码:

const typedOject: type123 = {...untypedObject} as type123;
typeObject.a = 1;

export interface type123 {
   a: number;
   b: number;
}
但编译器抱怨: 类型“any[]”上不存在属性“a”

那么,为什么新对象是any类型呢


感谢

这些类型的转换在处理传入的API数据时很常见

以下是我如何处理这些类型的转换:

类型/GithubRepo.ts
导出接口GithubRepo{
id:编号;
名称:字符串;
描述:字符串;
星星:数字;
}
export const convertIntoGithubRepo=(githubRepoFragment:any):GithubRepo=>({
id:githubreporagment.id,
名称:githubreporagment.name,
description:githubRepoFragment.description | |“”,
星星:githubreporagment.stargazers\u count,
});
用法

从“types/GithubRepo”导入{convertinotogithubrepo};
const githubRepo=转换为githubRepo(somegithubreporagment);
对于本例,我定义了一个类型,然后定义了将
any
类型的对象复制到
GithubRepo


在这个转换过程中,我们可以看到我正在检查
any
对象上是否存在描述,如果一个对象是falsy,则添加一个空白。对于这样的任务,我通常编写一个转换函数,它接受any对象并检查其属性,同时建立返回类型,我看不出在开发代码的上面代码中有
y
。修正了打字错误。我明白了。我已经添加了一个答案,如果您还有其他问题,请告诉我