You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The issue lies in using typeof event within the indexed access type. Since event is a parameter, typeof event resolves to just keyof IEventParamConfig, instead of the actual literal type used by the caller.
Proposed Solution
We can improve type safety by using a generic parameter to preserve the literal type:
Preserves the literal type of the event parameter through the generic constraint
Provides better type inference for the callback parameter
Maintains the same runtime behavior while improving compile-time type checking
Example Usage
// Before - Less precise typinguniverAPI.addEvent('DocCreated',(params)=>{// params type is less specific});// After - Precise typinguniverAPI.addEvent('DocCreated',(params)=>{// params is correctly typed to the specific event typeparams.unitId;// properly typedparams.doc;// properly typed});
Property 'id' does not exist on type 'ILifeCycleChangedEvent | IDocDisposedEvent | IDocCreatedParam | ICommandEvent | ISheetCreatedEventParams | ... 10 more ... | ICellEventParam'.ts(2339)
System information
No response
The text was updated successfully, but these errors were encountered:
Before you submit this issue, have you checked the following
Affected packages and versions
latest
Reproduction link
The current type definition for
addEvent
has a potential type safety issue:univer/packages/core/src/facade/f-univer.ts
Line 305 in 5d0b397
The issue lies in using
typeof event
within the indexed access type. Sinceevent
is a parameter,typeof event
resolves to justkeyof IEventParamConfig
, instead of the actual literal type used by the caller.Proposed Solution
We can improve type safety by using a generic parameter to preserve the literal type:
which results in:
Benefits
Example Usage
Expected behavior
should work
Actual behavior
raises
System information
No response
The text was updated successfully, but these errors were encountered: