Skip to content
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

fix(cdk): Angular v19 allowSignalWrites warning #9810

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 30 additions & 22 deletions projects/cdk/utils/miscellaneous/directive-binding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type {InjectOptions, ProviderToken, Signal, WritableSignal} from '@angular/core';
import {effect, inject, isSignal, signal} from '@angular/core';
import type {
CreateEffectOptions,
InjectOptions,
ProviderToken,
Signal,
WritableSignal,
} from '@angular/core';
import {effect, inject, isSignal, signal, VERSION} from '@angular/core';

type SignalLikeTypeOf<T> = T extends Signal<infer R> ? R : T;

Expand All @@ -14,34 +20,36 @@
key: G,
initial: I,
options: InjectOptions = {self: true},
): I extends Signal<any> ? I : WritableSignal<I> {

Check warning on line 23 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L23

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const result: any = isSignal(initial) ? initial : signal(initial);

Check warning on line 24 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L24

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const directive: any = inject(token, options);

Check warning on line 25 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L25

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
const output = directive[`${key.toString()}Change`];
const angularVersion = parseInt(VERSION.major, 10);
const isAngular19 = angularVersion >= 19;
const effectOptions: CreateEffectOptions = isAngular19
? {}
: {allowSignalWrites: true};

// TODO: Figure out why effects are executed all the time and not just when result changes (check with Angular 18)
let previous: any;

Check warning on line 34 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L34

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)

effect(
() => {
const value: any = result();

if (previous === value) {
return;
}

if (isSignal(directive[key])) {
directive[key].set(value);
} else {
directive[key] = value;
}

directive.ngOnChanges?.({});
output?.emit?.(value);
previous = value;
},
{allowSignalWrites: true},
);
effect(() => {
const value: any = result();

Check warning on line 37 in projects/cdk/utils/miscellaneous/directive-binding.ts

View check run for this annotation

codefactor.io / CodeFactor

projects/cdk/utils/miscellaneous/directive-binding.ts#L37

Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)
vladimirpotekhin marked this conversation as resolved.
Show resolved Hide resolved

if (previous === value) {
return;
}

if (isSignal(directive[key])) {
directive[key].set(value);
} else {
directive[key] = value;
}

directive.ngOnChanges?.({});
output?.emit?.(value);
previous = value;
}, effectOptions);

return result;
}
Loading