gpt4 book ai didi

typescript - 展开运算符类型不安全

转载 作者:行者123 更新时间:2023-12-05 02:10:10 55 4
gpt4 key购买 nike

我有一个函数应该返回某种类型,但使用扩展运算符导致它分配一个拼写错误的键。

interface State {
fieldA: number,
fieldB: string
}

const reducer: (state: State, action: {payload: string}) => State = (state, action) => {

// please note that those variables are not desired
const tmp = { ...state, feildB: action.payload }; // No compile time error, :(

// This is too verbose... but works
const tmp2 = Object.assign<State, Partial<State>>(state, {feildB: action.payload}) // ERROR - this is what I need
return tmp
}
const t = reducer({fieldA: 1, fieldB: 'OK'}, {payload: 'Misspelled'}) // Misspelled
console.log("feildB", (t as any).feildB) // Misspelled
console.log("fieldB", (t as any).fieldB) // OK

有没有办法使其类型安全,同时将样板代码保持在最低限度?

Playground code HERE

最佳答案

TypeScript 正在做它应该做的事情。在您的例子中,您正在创建一个新对象 tmp,其新类型具有 3 个字段,即:

interface State {
fieldA: number;
fieldB: string;
}
interface Tmp {
fieldA: string;
fieldB: string;
payload: string;
}

换句话说,传播运算符执行以下操作:

interface Obj {
[key: string]: any;
}

const spread = (...objects: Obj[]) => {
const merged: Obj = {};

objects.forEach(obj => {
Object.keys(obj).forEach(k => merged[k] = obj[k]);
});

return merged;
}

传播运算符正在为您创建一种新类型的对象;如果你想推断类型,那么你应该这样做:

// this now throws an error
const tmp: State = { ...state, feildB: action.payload };

关于typescript - 展开运算符类型不安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129738/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com