-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createAction() does not handle generic types correctly #4110
Comments
B.t.w, I wrapped with object type PcDataItem<K extends PcDataKey> = { key: K; data: PcDataType<K> };
const pcDataPropsFlatten = <K extends PcDataKey>(key: K, data: PcDataType<K>) =>
({ key, data } as PcDataItem<K>);
pcDataPropsFlatten('Ping', []); // works as expected with type checking for the data
const setPcDataTest = createAction('[Main] setPcData', pcDataPropsFlatten);
setPcDataTest('foo123', 'bar456'); // no type checking at all, both key,data are 'any' |
@tomer953 , try the following approach (playground): const pcDataProps2 = <K extends PcDataKey>(
obj: K extends PcDataKey ? { key: K; data: PcDataType<K> } : never
) => obj;
export const setPcData2 = createAction('[Main] setPcData', pcDataProps2);
pcDataProps2({ key: 'Drives', data: { response: 100 } }); // Invalid
pcDataProps2({ key: 'Ping', data: { response: 100 } }); // Valid
setPcData2({ key: 'Drives', data: { response: 100 } }); // Invalid
setPcData2({ key: 'Ping', data: { response: 100 } }); // Valid To be fair, I'm not exactly sure why it works (or rather, why your original approach does not). Probably, has something to do with better type narrowing due to |
Hm, my hunch is that your solution is introduce deference with a conditional type, making the generic type evaluated later. I think |
Which @ngrx/* package(s) are the source of the bug?
store
Minimal reproduction of the bug/regression with instructions
Full code, interfaces and such are here:
https://stackblitz.com/github/tomer953/ngrx-store-generic-types?file=src%2Fapp%2Fstore%2Factions.ts
Expected behavior
assume we have a function that use generic types to infer the arguments type, ie:
The
createAction
function should enforce strict typing based on the provided function, such that providing incorrect data types fordata
should result in a compilation error.so the signature should look like:
but instead is:
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s)
Angular CLI: 16.2.9
Node: 20.9.0 (Unsupported)
Package Manager: npm 10.1.0
OS: win32 x64
Angular: 16.2.12
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
@angular-devkit/architect 0.1602.9
@angular-devkit/build-angular 16.2.9
@angular-devkit/core 16.2.9
@angular-devkit/schematics 16.2.9
@angular/cli 16.2.9
@schematics/angular 16.2.9
rxjs 7.8.1
typescript 5.1.6
zone.js 0.13.3
@ngrx/store 16.3.0
Other information
No response
I would be willing to submit a PR to fix this issue
The text was updated successfully, but these errors were encountered: