Skip to content

Commit 0778e6f

Browse files
JoostKthePunderWoman
authored andcommittedJan 31, 2022
fix(compiler-cli): accept nullish coalescing operator for any and unknown types (#44862)
We should not make assumptions about the any and unknown types; using a nullish coalescing operator is acceptable for those. PR Close #44862
1 parent 25f8305 commit 0778e6f

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

Diff for: ‎packages/compiler-cli/src/ngtsc/typecheck/extended/checks/nullish_coalescing_not_nullable/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class NullishCoalescingNotNullableCheck extends
3535
return [];
3636
}
3737
const typeLeft = symbolLeft.tsType;
38+
if (typeLeft.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
39+
// We should not make assumptions about the any and unknown types; using a nullish coalescing
40+
// operator is acceptable for those.
41+
return [];
42+
}
43+
3844
// If the left operand's type is different from its non-nullable self, then it must
3945
// contain a null or undefined so this nullish coalescing operator is useful. No diagnostic to
4046
// report.

Diff for: ‎packages/compiler-cli/src/ngtsc/typecheck/extended/test/checks/nullish_coalescing_not_nullable/nullish_coalescing_not_nullable_spec.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ runInEachFileSystem(() => {
7474
expect(diags.length).toBe(0);
7575
});
7676

77+
it('should not produce nullish coalescing warning for the any type', () => {
78+
const fileName = absoluteFrom('/main.ts');
79+
const {program, templateTypeChecker} = setup([{
80+
fileName,
81+
templates: {
82+
'TestCmp': `{{ var1 ?? 'foo' }}`,
83+
},
84+
source: 'export class TestCmp { var1: any; }'
85+
}]);
86+
const sf = getSourceFileOrError(program, fileName);
87+
const component = getClass(sf, 'TestCmp');
88+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
89+
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
90+
{} /* options */);
91+
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
92+
expect(diags.length).toBe(0);
93+
});
94+
95+
it('should not produce nullish coalescing warning for the unknown type', () => {
96+
const fileName = absoluteFrom('/main.ts');
97+
const {program, templateTypeChecker} = setup([{
98+
fileName,
99+
templates: {
100+
'TestCmp': `{{ var1 ?? 'foo' }}`,
101+
},
102+
source: 'export class TestCmp { var1: unknown; }'
103+
}]);
104+
const sf = getSourceFileOrError(program, fileName);
105+
const component = getClass(sf, 'TestCmp');
106+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
107+
templateTypeChecker, program.getTypeChecker(), [nullishCoalescingNotNullableFactory],
108+
{} /* options */);
109+
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
110+
expect(diags.length).toBe(0);
111+
});
112+
77113
it('should not produce nullish coalescing warning for a type that includes undefined', () => {
78114
const fileName = absoluteFrom('/main.ts');
79115
const {program, templateTypeChecker} = setup([{
@@ -165,7 +201,7 @@ runInEachFileSystem(() => {
165201
},
166202
source: `
167203
export class TestCmp {
168-
func: (): string | null => null;
204+
func = (): string | null => null;
169205
}
170206
`,
171207
},

0 commit comments

Comments
 (0)
Please sign in to comment.