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: no longer emit error for functions has no return statement under isolatedDeclarations #58331

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/compiler/expressionToTypeNode.ts
Expand Up @@ -484,7 +484,9 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
if (declaration && !nodeIsMissing(declaration.body)) {
const body = declaration.body;
if (body && isBlock(body)) {
let hasReturn = false;
forEachReturnStatement(body, s => {
hasReturn = true;
if (!candidateExpr) {
candidateExpr = s.expression;
}
Expand All @@ -493,6 +495,7 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
return true;
}
});
if (!hasReturn) return true; // it's void
}
else {
candidateExpr = body;
Expand Down
@@ -1,8 +1,7 @@
child1.ts(9,17): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
parent.ts(1,1): error TS9026: Declaration emit for this file requires preserving this import for augmentations. This is not supported with --isolatedDeclarations.


==== child1.ts (1 errors) ====
==== child1.ts (0 errors) ====
import { ParentThing } from './parent';

declare module './parent' {
Expand All @@ -12,9 +11,6 @@ parent.ts(1,1): error TS9026: Declaration emit for this file requires preserving
}

export function child1(prototype: ParentThing) {
~~~~~~
!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9031 child1.ts:9:17: Add a return type to the function declaration.
prototype.add = (a: number, b: number) => a + b;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/isolatedDeclarationErrorsAugmentation.js
Expand Up @@ -29,3 +29,13 @@ child1(ParentThing.prototype);
export function child1(prototype) {
prototype.add = (a, b) => a + b;
}


//// [child1.d.ts]
import { ParentThing } from './parent';
declare module './parent' {
interface ParentThing {
add: (a: number, b: number) => number;
}
}
export declare function child1(prototype: ParentThing): void;
@@ -1,24 +1,24 @@
isolatedDeclarationErrorsClasses.ts(3,5): error TS9012: Property must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(4,5): error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(8,18): error TS7006: Parameter 'p' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(8,18): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(9,23): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(11,9): error TS9009: At least one accessor must have an explicit return type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(12,9): error TS7032: Property 'setOnly' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(12,17): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(36,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(36,6): error TS2304: Cannot find name 'missing'.
isolatedDeclarationErrorsClasses.ts(42,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(44,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(44,35): error TS7006: Parameter 'v' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(46,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,9): error TS7032: Property '[noParamAnnotationStringName]' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(48,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,39): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(50,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(55,5): error TS1169: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(56,5): error TS7010: '[noAnnotationLiteralName]', which lacks return-type annotation, implicitly has an 'any' return type.
isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't be inferred with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(12,18): error TS7006: Parameter 'p' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(12,18): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(13,23): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(15,9): error TS9009: At least one accessor must have an explicit return type annotation with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(16,9): error TS7032: Property 'setOnly' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(16,17): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(40,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(40,6): error TS2304: Cannot find name 'missing'.
isolatedDeclarationErrorsClasses.ts(46,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(48,35): error TS7006: Parameter 'v' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(50,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(52,9): error TS7032: Property '[noParamAnnotationStringName]' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
isolatedDeclarationErrorsClasses.ts(52,9): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations.
isolatedDeclarationErrorsClasses.ts(52,39): error TS7006: Parameter 'value' implicitly has an 'any' type.
isolatedDeclarationErrorsClasses.ts(54,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(59,5): error TS1169: A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
isolatedDeclarationErrorsClasses.ts(60,5): error TS7010: '[noAnnotationLiteralName]', which lacks return-type annotation, implicitly has an 'any' return type.
isolatedDeclarationErrorsClasses.ts(60,5): error TS9013: Expression type can't be inferred with --isolatedDeclarations.


==== isolatedDeclarationErrorsClasses.ts (21 errors) ====
Expand All @@ -28,28 +28,32 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b
~~~~~
!!! error TS9012: Property must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9029 isolatedDeclarationErrorsClasses.ts:3:5: Add a type annotation to the property field.
method() {}
method() { return console.log() }
~~~~~~
!!! error TS9008: Method must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9034 isolatedDeclarationErrorsClasses.ts:4:5: Add a return type to the method

methodImplicitVoid() {
if (Math.random() > 0.5) console.log()
else console.log()
}
methodOk(): void {}

methodParams(p): void {}
~
!!! error TS7006: Parameter 'p' implicitly has an 'any' type.
~
!!! error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9028 isolatedDeclarationErrorsClasses.ts:8:18: Add a type annotation to the parameter p.
!!! related TS9028 isolatedDeclarationErrorsClasses.ts:12:18: Add a type annotation to the parameter p.
methodParams2(p = 1 + 1): void {}
~~~~~
!!! error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9028 isolatedDeclarationErrorsClasses.ts:9:19: Add a type annotation to the parameter p.
!!! related TS9028 isolatedDeclarationErrorsClasses.ts:13:19: Add a type annotation to the parameter p.

get getOnly() { return 1 + 1 }
~~~~~~~
!!! error TS9009: At least one accessor must have an explicit return type annotation with --isolatedDeclarations.
!!! related TS9032 isolatedDeclarationErrorsClasses.ts:11:9: Add a return type to the get accessor declaration.
!!! related TS9032 isolatedDeclarationErrorsClasses.ts:15:9: Add a return type to the get accessor declaration.
set setOnly(value) { }
~~~~~~~
!!! error TS7032: Property 'setOnly' implicitly has type 'any', because its set accessor lacks a parameter type annotation.
Expand Down Expand Up @@ -83,7 +87,7 @@ isolatedDeclarationErrorsClasses.ts(56,5): error TS9013: Expression type can't b
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
~~~~~~~
!!! error TS2304: Cannot find name 'missing'.

[noAnnotationLiteralName](): void { }

[noParamAnnotationLiteralName](v: string): void { }
Expand Down
16 changes: 13 additions & 3 deletions tests/baselines/reference/isolatedDeclarationErrorsClasses.js
Expand Up @@ -4,8 +4,12 @@
export class Cls {

field = 1 + 1;
method() {}
method() { return console.log() }

methodImplicitVoid() {
if (Math.random() > 0.5) console.log()
else console.log()
}
methodOk(): void {}

methodParams(p): void {}
Expand Down Expand Up @@ -37,7 +41,7 @@ export class C {

// Should not be reported as an isolated declaration error
[missing] = 1;

[noAnnotationLiteralName](): void { }

[noParamAnnotationLiteralName](v: string): void { }
Expand All @@ -62,7 +66,13 @@ export interface I {
//// [isolatedDeclarationErrorsClasses.js]
export class Cls {
field = 1 + 1;
method() { }
method() { return console.log(); }
methodImplicitVoid() {
if (Math.random() > 0.5)
console.log();
else
console.log();
}
methodOk() { }
methodParams(p) { }
methodParams2(p = 1 + 1) { }
Expand Down