Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ namespace ts {
return true;
}
if (isUsedInFunctionOrInstanceProperty(usage, declaration, container)) {
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields) {
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields && getContainingClass(declaration)) {
return (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent)) &&
!isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [defineVariables_useDefineForClassFields.ts]
const a = () => b()
const b = () => null
a()

//// [defineVariables_useDefineForClassFields.js]
const a = () => b();
const b = () => null;
a();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
const a = () => b()
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))

const b = () => null
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))

a()
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
const a = () => b()
>a : () => any
>() => b() : () => any
>b() : any
>b : () => any

const b = () => null
>b : () => any
>() => null : () => any
>null : null

a()
>a() : any
>a : () => any

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting that this is the result type; I guess because --strictNullChecks is off by default in tests?

Might be better to change from () => null to () => 0 just to make sure the types propagate properly (or enable --strict)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think types matter that much for this test since it's just trying to show the absence of an error.


Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: ESNext
// @useDefineForClassFields: true

const a = () => b()
const b = () => null
a()