是否有一种方法可以让Typescript警告对象作为引用赋值被赋值到另一个对象



例如,在Typescript中允许这样做编辑:原来试图解决的原始代码示例。更好的例子是低于

const initObj = { a: 1, b:  2 }
const usableObj = {
x: initObj
}
usableObj.x.a = 2
const usableObj2 = {
x: initObj
}
console.log(usableObj2.x.a) // output is 2 but initObj.a = 1
// To get around this you have to assign via spread
const initObj = { a: 1, b: 2 }
const usableObj = {
x: {...initObj}
}
usableObj.x.a = 2
const usableObj2 = {
x: {...initObj}
}
console.log(usableObj.x.a) // output 2
console.log(usableObj2.x.a) // output 1

创建引用时可选的警告标志将很有帮助。我宁愿不使用创建额外编译代码的类来创建具有简单常量对象接口的类。给常量赋值是可以的,但是从常量对象创建引用可能需要警告。

在类上只读是很好的,但我不想在Redux中处理这些类,这就是问题发生的地方。由于我使用对象来保存处于状态的信息,这是一个错误,当"尝试"时可能会导致问题。复位状态。

这个警告有什么设置吗?

编辑:有实际问题的例子用例代码出现了,上面是一个更简单的例子,但是在与其他人给出的测试进行测试时没有同样的问题

// usage example (the base of where the issue was found)
interface InitObj {
a?: boolean
b?: number
}
const initObj: InitObj = {
a: false,
b: 1
}
interface IReducer {
init_obj: InitObj
}
const initReducer: IReducer = {
init_obj: initObj
}
interface IAction {
type: string
payload: InitObj
}
function Reducer(action: IAction, state: IReducer = initReducer) {
switch(action.type) {
case 'test1':
Object.assign(state.init_obj, {
...action.payload
})
return {...state}
default:
return state
}
}
let change1 = Reducer({ type: 'test1', payload: {a: true}}, initReducer)
console.log(`change1: ${JSON.stringify(change1)}`)
let change2 = Reducer({ type: 'test1', payload: {b: 2}}, change1)
console.log(`change2: ${JSON.stringify(change2)}`)
let reset = Reducer({type: 'test1', payload: initObj}, change2)
console.log(`Reset reducer: ${JSON.stringify(reset)}`) // should reset to { a: false, b: 1}

Object.freeze错误的新示例带有fix的示例-这很好,但希望对typescript

的赋值发出警告。

如果想要保证initObj的不变性,可以冻结它以防止进一步赋值。如果您希望仅在类型级别限制这一点,并在运行时允许(例如,可能出于性能原因),您可以正确地键入usableObj

const initObj = Object.freeze({ a: 1, b: 2 });
const usableObj = {
x: initObj
}
console.log('Regular obj assignment')
usableObj.x.a = 2
console.log(`Should be 1: ${initObj.a}`)
console.log('Assignment via spread')
const initObj2 = { a: 1, b: 2 }
const usableObj2 = {
x: {...initObj2}
}
usableObj2.x.a = 2
console.log(`should be 2: ${usableObj2.x.a}`)
console.log(`should be 1: ${initObj2.a}`)
<<ul>
  • 冷冻例子/gh>
    const initObj = { a: 1, b: 2 };
    type UsableObj = {
    x: Readonly<typeof initObj>;
    }
    const usableObj: UsableObj = {
    x: initObj
    }
    console.log('Regular obj assignment')
    usableObj.x.a = 2
    console.log(`Should be 1: ${initObj.a}`)
    console.log('Assignment via spread')
    const initObj2 = { a: 1, b: 2 }
    const usableObj2: UsableObj = {
    x: {...initObj2}
    }
    usableObj2.x.a = 2
    console.log(`should be 2: ${usableObj2.x.a}`)
    console.log(`should be 1: ${initObj2.a}`)
    
    • 输入示例
  • 最新更新