假设我有以下类型声明:
declare type Point2D = { x: number, y: number }
Run Code Online (Sandbox Code Playgroud)
我从服务器获取一些数据并获得以下信息:
const response = { x: 1.2, y: 3.4, foreign1: 'value', foreign2: 'value' }
Run Code Online (Sandbox Code Playgroud)
是否可以自动忽略不属于我的类型的所有属性?像这样:
const point: Point2D = response // Should skip all props except for 'x' and 'y'
Run Code Online (Sandbox Code Playgroud)
重要的是响应可以有任意数量的外来属性,所以我不能使用其余运算符的对象解构.