-
-
Notifications
You must be signed in to change notification settings - Fork 2k
select overload not match when enable strict mode in tsconfig #2780
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
Comments
Could this be because the store is types as - export class Store<T = object> extends Observable<T>
+ export class Store<T = any> extends Observable<T> @hanjeahwan you can solve the issue by typing the store in the component. - constructor(private store: Store) {}
+ constructor(private store: Store<any>) {}
// or
+ constructor(private store: Store<GameState>) {} |
On the NgRx Discord Server it was mentioned that typing the feature selector as follows should also work: - export const selectFeature = createFeatureSelector<AppState, GameState>(
- scoreboardFeatureKey
- );
+ export const selectFeature = createFeatureSelector<GameState>(
+ scoreboardFeatureKey
+ ); |
@timdeschryver thanks your help 😁 |
I just leave my experience as well, as I had a similar issue, solved thanks to Tim's suggestion. NX command to create a feature ngrx state generates automatically a "PartialState" that should be used explicit as Store type to solve the
|
I updated to the latest release (1.16.0), and the ES-Lint Message is changed according to the last PR. How this case should be handled if the Store should not have a generic type anymore? |
I feel like I'm missing something @pacoita. If you strongly type the feature that's selected by providing the entire required global state to There's no need to declare the global state shape anymore to |
The docs's selector page (https://ngrx.io/guide/store/selectors) only shows usage of |
I've added a PR for a clarification of this behavior in the docs: https://github.com/ngrx/platform/pull/3063/files |
I'm sorry I have to comment on this closed issue. But the problem persists for me. I'm using Angular 12.2.5 and NgRx (Store) 12.4.0. My component constructor currently has the following argument: If I remove the type
So essentially eslint is complaining that My question: What is the recommended solution here? I can disable the rule, but there seems to be a problem with this rule. If you would type the store to |
I'm making this follow-up comment, because I sort of made it work and then ran into the next problem. Nevertheless, I think this will help others, that run into the same problem. The following code is an example how I used to define my selectors. But this way I get compiler errors like the one mentioned earlier. export const selectFeature = (state: AppState) => state.feature;
export const selectFeatureCount = createSelector(
selectFeature,
(state: FeatureState) => state.counter
); This is the first thing you read in the documentation about selectors on https://ngrx.io/guide/store/selectors. But it gives you errors when used in conjunction with Angular and the default strict compiler mode. But it can be fixed by changing the first line to use export const selectFeature = createFeatureSelector<FeatureState>('feature');
export const selectFeatureCount = createSelector(
selectFeature,
(state: FeatureState) => state.counter
); This seems like a small change, but it took multiple hours to figure it out and it would be helpful if it was mentioned in the docs, because it is essential for Angular strict mode users (which is the default for new users). Worth mentioning, this leads to new problems. Since selectors with props are deprecated, it is recommended to use factory selectors instead. So I did, but strict mode forces me to define all the types, including return types. I didn't find any example on how to make a factory selector in the docs. But I did find examples in some of the issues here on GitHub. The examples are usually missing return types. So this is what I came up with. export const selectFoobar = (arg: number): MemoizedSelector<object, number> => {
return createSelector(selectFeature , (state: FeatureState) => {
return state.counter + arg;
});
}; The problem lies in |
@MartinMa Try using I do think the lint settings you're using are a bit harsh. Having to explicitly declare the types of everything is a real pain when type inference is so useful. // in reducer
const feature = createFeature({
name: 'feature',
reducer: createReducer(initialState),
});
// selector produced for free
feature.selectCount;
// in selectors
export const selectFoobar = (arg: number): MemoizedSelector<Record<string, unknown>, FeatureState> => {
return createSelector(feature.selectCount, (counter: number) => {
return counter + arg;
});
}; |
@timdeschryver I'm so sorry to bring this closed issue as well but how am I supposed to make this work; I'm confused after all the threads I've read at eslint-plugin-ngrx and here. I have the following login.selector.ts
login.component.ts
And I have the following error :
If I type my store object I have the following
|
@Yohandah is there a reason why everything is explicitly typed?
|
@timdeschryver I have tried to remove the types after I saw comments here and there, but my IDE was still showing the error unfortunately. I will try again tomorrow and then restart WebStorm maybe? If it works I will tweak my ESLint to deactivate this rule in *.selector.ts files |
For some reason, with the new function signature for |
@timdeschryver This is not working when I remove the type : This is my tsconfig with strict mode :
|
The first generic argument for // selector
export const selectIsAuthenticating: **MemoizedSelector<object, boolean>** = createSelector(
selectLoginState,
(state) => state.isAuthenticating,
);
// component
isAuthenticating$ = this.store.select(selectIsAuthenticating);
constructor(private readonly store: Store) {} |
@Yohandah from the screenshot, I can see that the types are still there. |
@timdeschryver @markostanimirovic Nice it's working after typing them with object then completely removed the types and disabled @typescript-eslint/typedef rule for .selector.ts files ! Thanks a lot ! |
I create a angular project with strict flag and try add in a feature store. But when I try to select the state with selector have created it appear overload not match. When I turn off strictFunctionTypes everything work well.
Minimal reproduction of the bug/regression with instructions:
Repo URL
Expected behavior:
No error
Versions of NgRx, Angular, Node, affected browser(s) and operating system(s):
NgRx : 10.0.1
Angular: 10.1.4
The text was updated successfully, but these errors were encountered: