Skip to content

Report assignability errors on the return/yield keywords #52943

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

Merged
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
11 changes: 9 additions & 2 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2179,13 +2179,14 @@ function getErrorSpanForArrowFunction(sourceFile: SourceFile, node: ArrowFunctio
export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpan {
let errorNode: Node | undefined = node;
switch (node.kind) {
case SyntaxKind.SourceFile:
case SyntaxKind.SourceFile: {
const pos = skipTrivia(sourceFile.text, 0, /*stopAfterLineBreak*/ false);
if (pos === sourceFile.text.length) {
// file is empty - return span for the beginning of the file
return createTextSpan(0, 0);
}
return getSpanOfTokenAtPosition(sourceFile, pos);
}
// This list is a work in progress. Add missing node kinds to improve their error
// spans.
case SyntaxKind.VariableDeclaration:
Expand All @@ -2210,10 +2211,16 @@ export function getErrorSpanForNode(sourceFile: SourceFile, node: Node): TextSpa
case SyntaxKind.ArrowFunction:
return getErrorSpanForArrowFunction(sourceFile, node as ArrowFunction);
case SyntaxKind.CaseClause:
case SyntaxKind.DefaultClause:
case SyntaxKind.DefaultClause: {
const start = skipTrivia(sourceFile.text, (node as CaseOrDefaultClause).pos);
const end = (node as CaseOrDefaultClause).statements.length > 0 ? (node as CaseOrDefaultClause).statements[0].pos : (node as CaseOrDefaultClause).end;
return createTextSpanFromBounds(start, end);
}
case SyntaxKind.ReturnStatement:
case SyntaxKind.YieldExpression: {
const pos = skipTrivia(sourceFile.text, (node as ReturnStatement | YieldExpression).pos);
return getSpanOfTokenAtPosition(sourceFile, pos);
}
}

if (errorNode === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ tests/cases/compiler/accessors_spec_section-4.5_error-cases.ts(11,51): error TS2
class LanguageSpec_section_4_5_error_cases {
public set AnnotatedSetter_SetterFirst(a: number) { }
public get AnnotatedSetter_SetterFirst() { return ""; }
~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

public get AnnotatedSetter_SetterLast() { return ""; }
~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
public set AnnotatedSetter_SetterLast(a: number) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/file.js(19,3): error TS2
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<string>'?
const c = async () => {
return 0
~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}

Expand Down
11 changes: 3 additions & 8 deletions tests/baselines/reference/baseConstraintOfDecorator.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ tests/cases/compiler/baseConstraintOfDecorator.ts(12,18): error TS2545: A mixin
==== tests/cases/compiler/baseConstraintOfDecorator.ts (3 errors) ====
export function classExtender<TFunction>(superClass: TFunction, _instanceModifier: (instance: any, args: any[]) => void): TFunction {
return class decoratorFunc extends superClass {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
!!! error TS2322: 'TFunction' could be instantiated with an arbitrary type which could be unrelated to 'typeof decoratorFunc'.
~~~~~~~~~~
!!! error TS2507: Type 'TFunction' is not a constructor function type.
!!! related TS2735 tests/cases/compiler/baseConstraintOfDecorator.ts:1:31: Did you mean for 'TFunction' to be constrained to type 'new (...args: any[]) => unknown'?
constructor(...args: any[]) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
super(...args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~
_instanceModifier(this, args);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
~~~~~~~~~
};
~~~~~~
!!! error TS2322: Type 'typeof decoratorFunc' is not assignable to type 'TFunction'.
!!! error TS2322: 'TFunction' could be instantiated with an arbitrary type which could be unrelated to 'typeof decoratorFunc'.
}

class MyClass { private x; }
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/checkJsdocReturnTag2.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ tests/cases/conformance/jsdoc/returns.js(13,5): error TS2322: Type 'number | boo
*/
function f() {
return 5;
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}

Expand All @@ -21,7 +21,7 @@ tests/cases/conformance/jsdoc/returns.js(13,5): error TS2322: Type 'number | boo
*/
function f1() {
return 5 || true;
~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number | boolean' is not assignable to type 'string | number'.
!!! error TS2322: Type 'boolean' is not assignable to type 'string | number'.
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/checkJsdocTypeTag5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ tests/cases/conformance/jsdoc/test.js(34,5): error TS2322: Type '1 | 2' is not a
// all 6 should error on return statement/expression
/** @type {(x: number) => string} */
function h(x) { return x }
~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
/** @type {(x: number) => string} */
var f = x => x
~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
/** @type {(x: number) => string} */
var g = function (x) { return x }
~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.

/** @type {{ (x: number): string }} */
function i(x) { return x }
~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
/** @type {{ (x: number): string }} */
var j = x => x
~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
/** @type {{ (x: number): string }} */
var k = function (x) { return x }
~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tests/cases/conformance/jsdoc/0.js(22,22): error TS2345: Argument of type 'strin
/** @type {function(number): number} */
method1(n1) {
return "42";
~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
},
/** @type {function(number): number} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tests/cases/compiler/complicatedIndexedAccessKeyofReliesOnKeyofNeverUpperBound.t
export function makeNewChannel<T extends ChannelType>(type: T): NewChannel<ChannelOfType<T>> {
const localChannelId = `blahblahblah`;
return { type, localChannelId };
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type '{ type: T; localChannelId: string; }' is not assignable to type 'NewChannel<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>>'.
!!! error TS2322: Type '{ type: T; localChannelId: string; }' is not assignable to type 'Pick<ChannelOfType<T, TextChannel> | ChannelOfType<T, EmailChannel>, "type">'.
!!! error TS2322: Types of property 'type' are incompatible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T

function f3<Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> {
return x;
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'.
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'.
}
Expand All @@ -142,7 +142,7 @@ tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(116,3): error T
x: Q
): InferBecauseWhyNotDistributive<Q> {
return x; // should fail
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNotDistributive<Q>'.
!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNotDistributive<Q>'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts(25,9): error TS23
[G.A]() { }
get [G.B]() {
return true;
~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'boolean' is not assignable to type 'number'.
}
set [G.B](x: number) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ tests/cases/compiler/constructorReturnsInvalidType.ts(3,9): error TS2409: Return
class X {
constructor() {
return 1;
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'X'.
~~~~~~~~~
~~~~~~
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
}
foo() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignabl
x: number;
constructor() {
return 1; // error
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'D'.
~~~~~~~~~
~~~~~~
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
}
}
Expand All @@ -36,7 +36,7 @@ tests/cases/conformance/classes/constructorDeclarations/constructorWithAssignabl
x: T;
constructor() {
return { x: 1 }; // error
~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2409: Return type of constructor signature must be assignable to the instance type of the class.
~
!!! error TS2322: Type 'number' is not assignable to type 'T'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ tests/cases/conformance/classes/members/inheritanceAndOverriding/derivedGenericC
class E<T extends string> extends D {
x: T;
get X(): T { return ''; } // error
~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
foo(): T {
return ''; // error
~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'T'.
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts(3,3): error
async function foo<T>(x: T): Promise<T> {
let yaddable = await getXOrYadda(x);
return yaddable;
~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'Yadda | Awaited<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Yadda | Awaited<T>'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ tests/cases/compiler/emptyObjectNotSubtypeOfIndexSignatureContainingObject1.ts(4
const result = foos == null ? {} : mapValues(foos, f => f.foo);
// This line _should_ fail, because `result` is not the right type.
return result;
~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'Dictionary<string>' is not assignable to type 'Record<string, Bar>'.
!!! error TS2322: 'string' index signatures are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'Bar'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ tests/cases/compiler/emptyObjectNotSubtypeOfIndexSignatureContainingObject2.ts(4
const result = foos == null ? {} : mapValues(foos, f => f.foo);
// This line _should_ fail, because `result` is not the right type.
return result;
~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'Dictionary<string>' is not assignable to type 'Record<string, Bar>'.
!!! error TS2322: 'string' index signatures are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'Bar'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning
/** @type {FunctionReturningPromise} */
async function testPromise2() {
return "asd";
~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}

Expand Down Expand Up @@ -56,7 +56,7 @@ tests/cases/conformance/jsdoc/foo.js(45,18): error TS2534: A function returning
/** @type {FunctionReturningNever} */
async function testNever2() {
return "asd";
~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'never'.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tests/cases/compiler/bar.ts(4,31): error TS2523: 'yield' expressions cannot be u
}

export function* foo2({ foo = yield "a" }) {
~~~~~~~~~
~~~~~
!!! error TS2523: 'yield' expressions cannot be used in a parameter initializer.
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tests/cases/conformance/generators/generatorExplicitReturnType.ts(16,11): error
~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
return 10; // error
~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tests/cases/conformance/generators/generatorReturnContextualType.ts(34,3): error
async function* f4(): AsyncGenerator<any, { x: 'x' }, any> {
const ret = { x: 'x' };
return Promise.resolve(ret); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type '{ x: string; }' is not assignable to type '{ x: "x"; }'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type '"x"'.
Expand All @@ -45,7 +45,7 @@ tests/cases/conformance/generators/generatorReturnContextualType.ts(34,3): error
async function* g4(): AsyncIterator<any, { x: 'x' }, any> {
const ret = { x: 'x' };
return Promise.resolve(ret); // Error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type '{ x: string; }' is not assignable to type '{ x: "x"; }'.
!!! error TS2322: Types of property 'x' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type '"x"'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ tests/cases/conformance/generators/generatorReturnTypeInference.ts(72,15): error

function* g204() { // Generator<number, void, any>
const x = f2(yield 1);
~~~~~~~
~~~~~
!!! error TS7057: 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ tests/cases/conformance/generators/generatorReturnTypeInferenceNonStrict.ts(131,

function* g204() { // Generator<number, void, any>
const x = f2(yield 1);
~~~~~~~
~~~~~
!!! error TS7057: 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tests/cases/conformance/expressions/contextualTyping/getSetAccessorContextualTyp
set X(x: number) { }
get X() {
return "string"; // Error; get contextual type by set accessor parameter type annotation
~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ tests/cases/compiler/main.ts(4,5): error TS2343: This syntax requires an importe
await 1;
yield 2;
yield* [3];
~~~~~~~~~~
~~~~~
!!! error TS2343: This syntax requires an imported helper named '__asyncDelegator' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
~~~~~~~~~~
~~~~~
!!! error TS2343: This syntax requires an imported helper named '__asyncValues' which does not exist in 'tslib'. Consider upgrading your version of 'tslib'.
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/inferSetterParamType.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tests/cases/compiler/inferSetterParamType.ts(13,9): error TS2322: Type 'number'

get bar() {
return 0; // should be an error - can't coerce infered return type to match setter annotated type
~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'string'.
}
set bar(n:string) {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/invalidReturnStatements.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts(1
name: string;
}
function fn10(): D { return { id: 12 }; }
~~~~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2739: Type '{ id: number; }' is missing the following properties from type 'D': name, dispose

function fn11(): D { return new C(); }
~~~~~~~~~~~~~~~
~~~~~~
!!! error TS2741: Property 'name' is missing in type 'C' but required in type 'D'.
!!! related TS2728 tests/cases/conformance/statements/returnStatements/invalidReturnStatements.ts:14:5: 'name' is declared here.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23

function get123<K extends keyof Type>(): Type[K] {
return 123; // Error
~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type '123' is not assignable to type 'Type[K]'.
!!! error TS2322: Type 'number' is not assignable to type 'never'.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tests/cases/compiler/matchReturnTypeInAllBranches.ts(30,13): error TS2322: Type
else
{
return 12345;
~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'number' is not assignable to type 'boolean'.
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/narrowByEquality.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ tests/cases/compiler/narrowByEquality.ts(55,9): error TS2322: Type 'string | num
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
return level;
~~~~~~~~~~~~~
~~~~~~
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
!!! error TS2322: Type 'string' is not assignable to type 'number'.
}
Expand Down
Loading