Skip to content

Commit b558a01

Browse files
devversionpkozlowski-opensource
authored andcommitted
fix(core): generic inference for signal inputs may break with --strictFunctionTypes (#54652)
This commit fixes that the generic inference for signal inputs may break with `--strictFunctionTypes`. We are not using --strictFunctionTypes` in all tests, so function parameters are always checked bivariantly- while in 1P function parameters are checked contravariantly. This breaks sub-typing and e.g. `InputSignal<number>` is not a subtype of `InputSignalWithTransform<unknown, number>`. Root cause is that the input signal captures the equality function that is exclusively concerned with the `ReadT`. And `ReadT` is never equal, or a supertype of unknown. https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgSSTAFcYBlBAcyUwBsB1BGACwBUpMkBndaAWwA8AJWCYAJqwA0celCbBWAPjgBvAFBw4Aei1xWACXxlEXOCzxQIEeNkzEuwAIRmAnmDxhMHPsFRRTXnjYzMDYANbAYnDYECgcAG5eCJwwtC7SAHIA8qxwAEYIALSJcilpAHQacMAAjsR0AFxwABSYTSLiUvntohIAlHAAvMp5VrSiSADcalUA+rNQvaw9ndOa8wDucqjLMtsK0wC+aqCQsIgoaFi4BESkFNR0wkuKVaCoSGKmhCTkVDQMJhsDjcXhQQQdCTSSFKVTHNQxbjwEBNH73f50RgsdicHj8ATEJBhJAQDZIaRIYh8PJoZSDKqU2i0ZyYb53P6PWgCSnU2nTKpaABUM00rDceAA5GiOQDuVSaVBFBKTHASfBWVwMXlxmYIK53HApeyHgCscDcWDBITiaTyar5bSJZVReLTBB0HAwJZ3LAXIbavVaMrAhcYnxPDAENrgM7NHpxYbWk0eQrpHlkw6oANhvkxhNlQhTGq4BqtTqYHqYAmJUm4NaSWS00167bsyM85wnVU4-H3G6PZ5vL40KYJZhg59DeOS4tQxBw5hI9HYz2XQaJS2yQWi9YS1xNY9o7r9ZKU2gnc0AEwzDTZVgAUSaABF7wAFe8ZF8ZXJZDJwAAGhSFFwMByNgMAAGKEuBCCxGKfb-nA6C0JglArqugpaEAA PR Close #54652
1 parent 2fc1971 commit b558a01

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

packages/compiler-cli/src/ngtsc/testing/fake_core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export interface InputFunction {
189189
export const input: InputFunction = null!;
190190

191191
export type ɵUnwrapInputSignalWriteType<Field> =
192-
Field extends InputSignalWithTransform<unknown, infer WriteT>? WriteT : never;
192+
Field extends InputSignalWithTransform<any, infer WriteT>? WriteT : never;
193193
export type ɵUnwrapDirectiveSignalInputs<Dir, Fields extends keyof Dir> = {
194194
[P in Fields]: ɵUnwrapInputSignalWriteType<Dir[P]>
195195
};

packages/compiler-cli/src/ngtsc/typecheck/test/test_case_helper.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import ts from 'typescript';
10+
911
import {TypeCheckingConfig} from '../api';
1012
import {diagnose} from '../testing';
1113

@@ -54,7 +56,7 @@ export interface TestCase {
5456
* Diagnoses the given test case, by constructing the test TypeScript file
5557
* and running the type checker on it.
5658
*/
57-
export function typeCheckDiagnose(c: TestCase) {
59+
export function typeCheckDiagnose(c: TestCase, compilerOptions?: ts.CompilerOptions) {
5860
const inputs = c.inputs ?? {};
5961
const outputs = c.outputs ?? {};
6062

@@ -124,7 +126,7 @@ export function typeCheckDiagnose(c: TestCase) {
124126
.map(([name]) => name),
125127
},
126128
],
127-
undefined, c.options);
129+
undefined, c.options, compilerOptions);
128130

129131
expect(messages).toEqual(c.expected);
130132
}
@@ -136,4 +138,12 @@ export function generateDiagnoseJasmineSpecs(cases: TestCase[]): void {
136138
typeCheckDiagnose(c);
137139
});
138140
}
141+
142+
describe('with `--strict`', () => {
143+
for (const c of cases) {
144+
(c.focus ? fit : it)(c.id, () => {
145+
typeCheckDiagnose(c, {strict: true});
146+
});
147+
}
148+
});
139149
}

packages/core/src/authoring/input/input_type_checking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {InputSignalWithTransform} from './input_signal';
1010

1111
/** Retrieves the `WriteT` of an `InputSignal` and `InputSignalWithTransform`. */
1212
export type ɵUnwrapInputSignalWriteType<Field> =
13-
Field extends InputSignalWithTransform<unknown, infer WriteT>? WriteT : never;
13+
Field extends InputSignalWithTransform<any, infer WriteT>? WriteT : never;
1414

1515
/**
1616
* Unwraps all `InputSignal`/`InputSignalWithTransform` class fields of

0 commit comments

Comments
 (0)