Skip to content

Commit

Permalink
feat(ng-core): improve type safety of WrappedControlSuperclass
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Subclasses of `WrappedControlSuperclass` may need to be adjusted if they were doing any type-unsafe things. For example you may need to add `| null` to its typing, or create your form control with the option `nonNullable: true`.
  • Loading branch information
ersimont committed Apr 15, 2023
1 parent bd78e3e commit da5e888
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 144 deletions.
15 changes: 10 additions & 5 deletions projects/ng-core/src/lib/forms/control-synchronizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { delayWhen, Observable, Subject } from 'rxjs';
import {
provideValueAccessor,
WrappedControlSuperclass,
WrappedFormControlSuperclass,
} from '../../public-api';
import { find, findDirective, setValue } from '../../test-helpers';

abstract class AbstractValidatingComponent extends WrappedControlSuperclass<string> {
abstract class AbstractValidatingComponent extends WrappedControlSuperclass<
string,
string | null
> {
syncError = false;
control = new FormControl(undefined, () =>
control = new FormControl<string | null>(null, () =>
this.#makeError(this.syncError, 'Sync'),
);
failOnNeedlessAsync = false;
Expand Down Expand Up @@ -303,7 +305,9 @@ describe('ControlSynchronizer', () => {
template: `<input [formControl]="control" />`,
providers: [provideValueAccessor(InnerComponent)],
})
class InnerComponent extends WrappedFormControlSuperclass<string> {
class InnerComponent extends WrappedControlSuperclass<string | null> {
control = new FormControl('');

protected override outerToInnerErrors(
errors: ValidationErrors,
): ValidationErrors {
Expand All @@ -317,8 +321,9 @@ describe('ControlSynchronizer', () => {
<sl-inner *ngIf="showInner" [formControl]="control"></sl-inner>
`,
})
class OuterComponent extends WrappedFormControlSuperclass<string> {
class OuterComponent extends WrappedControlSuperclass<string | null> {
@Input() showInner!: boolean;
control = new FormControl('');
}

const ctx = new ComponentContext(OuterComponent, {
Expand Down
2 changes: 1 addition & 1 deletion projects/ng-core/src/lib/forms/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { FormComponentSuperclass } from './form-component-superclass';
export { provideValueAccessor } from './provide-value-accessor';
export { WrappedControlSuperclass } from './wrapped-control-superclass';
export { WrappedFormControlSuperclass } from './wrapped-form-control-superclass';
export { provideValueAccessor } from './provide-value-accessor';
Loading

0 comments on commit da5e888

Please sign in to comment.