Skip to content

Commit 73eb55c

Browse files
feat: convert entity and router selectors interfaces to types (#3853)
1 parent 999dcb6 commit 73eb55c

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expecter } from 'ts-snippet';
2+
import { compilerOptions } from './utils';
3+
4+
describe('EntitySelectors', () => {
5+
const expectSnippet = expecter(
6+
(code) => `
7+
import { Selector } from '@ngrx/store';
8+
import { EntitySelectors } from './modules/entity/src/models';
9+
10+
${code}
11+
`,
12+
compilerOptions()
13+
);
14+
15+
it('is compatible with a dictionary of selectors', () => {
16+
expectSnippet(`
17+
type SelectorsDictionary = Record<
18+
string,
19+
| Selector<Record<string, any>, unknown>
20+
| ((...args: any[]) => Selector<Record<string, any>, unknown>)
21+
>;
22+
type ExtendsSelectorsDictionary<T> = T extends SelectorsDictionary
23+
? true
24+
: false;
25+
26+
let result: ExtendsSelectorsDictionary<
27+
EntitySelectors<unknown, Record<string, any>>
28+
>;
29+
`).toInfer('result', 'true');
30+
});
31+
});

modules/entity/spec/types/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const compilerOptions = () => ({
2+
moduleResolution: 'node',
3+
target: 'ES2022',
4+
baseUrl: '.',
5+
experimentalDecorators: true,
6+
strict: true,
7+
paths: {
8+
'@ngrx/entity': ['./modules/entity'],
9+
'@ngrx/store': ['./modules/store'],
10+
},
11+
});

modules/entity/src/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ export interface EntityStateAdapter<T> {
7878
map<S extends EntityState<T>>(map: EntityMap<T>, state: S): S;
7979
}
8080

81-
export interface EntitySelectors<T, V> {
81+
export type EntitySelectors<T, V> = {
8282
selectIds: (state: V) => string[] | number[];
8383
selectEntities: (state: V) => Dictionary<T>;
8484
selectAll: (state: V) => T[];
8585
selectTotal: (state: V) => number;
86-
}
86+
};
8787

8888
export interface EntityAdapter<T> extends EntityStateAdapter<T> {
8989
selectId: IdSelector<T>;

modules/entity/src/state_selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createSelector } from '@ngrx/store';
2-
import { EntityState, EntitySelectors, Dictionary } from './models';
2+
import { EntityState, EntitySelectors } from './models';
33

44
export function createSelectorsFactory<T>() {
55
function getSelectors(): EntitySelectors<T, EntityState<T>>;

modules/router-store/spec/types/selectors.spec.ts renamed to modules/router-store/spec/types/router_selectors.types.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expecter } from 'ts-snippet';
22
import { compilerOptions } from './utils';
33

4-
describe('router selectors', () => {
4+
describe('getRouterSelectors', () => {
55
const expectSnippet = expecter(
66
(code) => `
77
import { getRouterSelectors, RouterReducerState } from '@ngrx/router-store';
@@ -124,3 +124,32 @@ describe('router selectors', () => {
124124
);
125125
});
126126
});
127+
128+
describe('RouterStateSelectors', () => {
129+
const expectSnippet = expecter(
130+
(code) => `
131+
import { Selector } from '@ngrx/store';
132+
import { RouterStateSelectors } from './modules/router-store/src/models';
133+
134+
${code}
135+
`,
136+
compilerOptions()
137+
);
138+
139+
it('is compatible with a dictionary of selectors', () => {
140+
expectSnippet(`
141+
type SelectorsDictionary = Record<
142+
string,
143+
| Selector<Record<string, any>, unknown>
144+
| ((...args: any[]) => Selector<Record<string, any>, unknown>)
145+
>;
146+
type ExtendsSelectorsDictionary<T> = T extends SelectorsDictionary
147+
? true
148+
: false;
149+
150+
let result: ExtendsSelectorsDictionary<
151+
RouterStateSelectors<Record<string, any>>
152+
>;
153+
`).toInfer('result', 'true');
154+
});
155+
});

modules/router-store/src/models.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Data, Params } from '@angular/router';
22
import { MemoizedSelector } from '@ngrx/store';
33

4-
export interface RouterStateSelectors<V> {
4+
export type RouterStateSelectors<V> = {
55
selectCurrentRoute: MemoizedSelector<V, any>;
66
selectFragment: MemoizedSelector<V, string | undefined>;
77
selectQueryParams: MemoizedSelector<V, Params>;
@@ -14,4 +14,4 @@ export interface RouterStateSelectors<V> {
1414
) => MemoizedSelector<V, string | undefined>;
1515
selectUrl: MemoizedSelector<V, string>;
1616
selectTitle: MemoizedSelector<V, string | undefined>;
17-
}
17+
};

0 commit comments

Comments
 (0)