feat!: use strict query params#331
Merged
Merged
Conversation
Array query parameters like `?size=m&size=l&size=xl` are now correctly resolved to `readonly string[]` instead of `string`.
**BREAKING CHANGES**
`RouterStore#queryParams$` and `MinimalActivatedRouteSnapshot#queryParams` use `StrictQueryParams` instead of `StrictRouteParams`. Members are of type `string | readonly string[] | undefined` instead of `string | undefined`.
The TypeScript compiler will fail to compile code that does not handle the string array type.
BEFORE:
```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";
@component({
// (...)
})
export class ShirtsComponent {
#routerStore = inject(RouterStore);
size$: Observable<string> = this.#routerStore.queryParams$.pipe(
map((params) => params["size"]),
);
}
```
AFTER:
```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";
@component({
// (...)
})
export class ShirtsComponent {
#routerStore = inject(RouterStore);
size$: Observable<readonly string[]> = this.#routerStore.queryParams$.pipe(
map((params) => params["size"]),
map((size) => (Array.isArray(size) ? size : [size]))
);
}
```
`RouterStore#selectQueryParam` use `StrictQueryParams` instead of `StrictRouteParams`. The returned value is of type `string | readonly string[] | undefined` instead of `string | undefined`.
The TypeScript compiler will fail to compile code that does not handle the string array type.
BEFORE:
```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";
@component({
// (...)
})
export class ShirtsComponent {
#routerStore = inject(RouterStore);
size$: Observable<string> = this.#routerStore.selectQueryParam('size');
}
```
AFTER:
```typescript
// shirts.component.ts
// (...)
import { RouterStore } from "@ngworker/router-component-store";
@component({
// (...)
})
export class ShirtsComponent {
#routerStore = inject(RouterStore);
size$: Observable<readonly string[]> = this.#routerStore.selectQueryParam('size').pipe(
map((size) => (Array.isArray(size) ? size : [size]))
);
}
```
5c37c02 to
d8f093d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Features
StrictQueryParamsfor query parameters instead ofStrictRouteParamsArray query parameters like
?size=m&size=l&size=xlare now correctly resolved toreadonly string[]instead ofstring.BREAKING CHANGES
RouterStore#queryParams$andMinimalActivatedRouteSnapshot#queryParamsuseStrictQueryParamsinstead ofStrictRouteParams. Members are of typestring | readonly string[] | undefinedinstead ofstring | undefined.The TypeScript compiler will fail to compile code that does not handle the string array type.
BEFORE:
AFTER:
RouterStore#selectQueryParamuseStrictQueryParamsinstead ofStrictRouteParams. The returned value is of typestring | readonly string[] | undefinedinstead ofstring | undefined.The TypeScript compiler will fail to compile code that does not handle the string array type.
BEFORE:
AFTER: