From 8a28e151460df8e26ad161e61637c8dfbca2a847 Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 31 May 2017 12:05:31 -0700 Subject: [PATCH 1/6] Get jsDoctype for property assignment --- src/compiler/checker.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c5afb367cf4cc..87aa6dc8d3c55 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -13273,6 +13273,7 @@ namespace ts { let patternWithComputedProperties = false; let hasComputedStringProperty = false; let hasComputedNumberProperty = false; + const isInJSFile = isInJavaScriptFile(node); let offset = 0; for (let i = 0; i < node.properties.length; i++) { @@ -13281,6 +13282,11 @@ namespace ts { if (memberDecl.kind === SyntaxKind.PropertyAssignment || memberDecl.kind === SyntaxKind.ShorthandPropertyAssignment || isObjectLiteralMethod(memberDecl)) { + let jsdocType: Type; + if (isInJSFile) { + jsdocType = getTypeForDeclarationFromJSDocComment(memberDecl); + } + let type: Type; if (memberDecl.kind === SyntaxKind.PropertyAssignment) { type = checkPropertyAssignment(memberDecl, checkMode); @@ -13293,6 +13299,11 @@ namespace ts { type = checkExpressionForMutableLocation((memberDecl).name, checkMode); } + if (jsdocType) { + checkTypeAssignableTo(type, jsdocType, memberDecl); + type = jsdocType; + } + typeFlags |= type.flags; const prop = createSymbol(SymbolFlags.Property | member.flags, member.name); if (inDestructuringPattern) { From 558cb2e2faaae17043664666ae8b12f15d5f89e7 Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 31 May 2017 12:15:50 -0700 Subject: [PATCH 2/6] Add tests --- .../checkJsdocTypeTagOnObjectProperty1.ts | 24 +++++++++++++++++++ .../checkJsdocTypeTagOnObjectProperty2.ts | 20 ++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts create mode 100644 tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts new file mode 100644 index 0000000000000..b45f3f0cc5188 --- /dev/null +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts @@ -0,0 +1,24 @@ +// @allowJS: true +// @suppressOutputPathCheck: true +// @strictNullChecks: true + +// @filename: 0.js +// @ts-check +var lol = "hello Lol" +const obj = { + /** @type {string|undefined} */ + foo: undefined, + /** @type {string|undefined} */ + bar: 42, + /** @type {function(number): number} */ + method1(n1) { + return n1 + 42; + }, + /** @type {string} */ + lol +} +obj.foo = 'string' +obj.foo; +obj.lol +obj.bar = undefined; +var k = obj.method1(0); \ No newline at end of file diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts new file mode 100644 index 0000000000000..262858381374c --- /dev/null +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts @@ -0,0 +1,20 @@ +// @allowJS: true +// @suppressOutputPathCheck: true +// @strictNullChecks: true + +// @filename: 0.js +// @ts-check +const obj = { + /** @type {string|undefined} */ + foo: undefined, + /** @type {string|undefined} */ + bar: 42, + /** @type {function(number): number} */ + method1(n1) { + return (n1 + 42).toString() + }, + /** @type {string} */ + lol +} +var lol = "string" +obj.foo = 5 From 1daff75d466b73afd5a2a48a2c5c23b955e12202 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 31 May 2017 13:13:25 -0700 Subject: [PATCH 3/6] Update tests/baselines --- .../checkJsdocTypeTagOnObjectProperty1.js | 47 ++++++++++++ ...checkJsdocTypeTagOnObjectProperty1.symbols | 58 ++++++++++++++ .../checkJsdocTypeTagOnObjectProperty1.types | 76 +++++++++++++++++++ ...ckJsdocTypeTagOnObjectProperty2.errors.txt | 39 ++++++++++ .../checkJsdocTypeTagOnObjectProperty2.js | 37 +++++++++ .../checkJsdocTypeTagOnObjectProperty1.ts | 10 ++- .../checkJsdocTypeTagOnObjectProperty2.ts | 12 +-- 7 files changed, 270 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js create mode 100644 tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols create mode 100644 tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types create mode 100644 tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt create mode 100644 tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js new file mode 100644 index 0000000000000..79f5d7bfd9aa9 --- /dev/null +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js @@ -0,0 +1,47 @@ +//// [0.js] +// @ts-check +var lol = "hello Lol" +const obj = { + /** @type {string|undefined} */ + foo: undefined, + /** @type {string|undefined} */ + bar: "42", + /** @type {function(number): number} */ + method1(n1) { + return n1 + 42; + }, + /** @type {string} */ + lol, + /** @type {number} */ + ['b' + 'ar1']: 42, +} +obj.foo = 'string' +obj.lol +obj.bar = undefined; +var k = obj.method1(0); +obj.bar1 = "42"; + +//// [0.js] +// @ts-check +var lol = "hello Lol"; +var obj = (_a = { + /** @type {string|undefined} */ + foo: undefined, + /** @type {string|undefined} */ + bar: "42", + /** @type {function(number): number} */ + method1: function (n1) { + return n1 + 42; + }, + /** @type {string} */ + lol: lol + }, + /** @type {number} */ + _a['b' + 'ar1'] = 42, + _a); +obj.foo = 'string'; +obj.lol; +obj.bar = undefined; +var k = obj.method1(0); +obj.bar1 = "42"; +var _a; diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols new file mode 100644 index 0000000000000..9a1b1acb8a898 --- /dev/null +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols @@ -0,0 +1,58 @@ +=== tests/cases/conformance/jsdoc/0.js === +// @ts-check +var lol = "hello Lol" +>lol : Symbol(lol, Decl(0.js, 1, 3)) + +const obj = { +>obj : Symbol(obj, Decl(0.js, 2, 5)) + + /** @type {string|undefined} */ + foo: undefined, +>foo : Symbol(foo, Decl(0.js, 2, 13)) +>undefined : Symbol(undefined) + + /** @type {string|undefined} */ + bar: "42", +>bar : Symbol(bar, Decl(0.js, 4, 17)) + + /** @type {function(number): number} */ + method1(n1) { +>method1 : Symbol(method1, Decl(0.js, 6, 12)) +>n1 : Symbol(n1, Decl(0.js, 8, 10)) + + return n1 + 42; +>n1 : Symbol(n1, Decl(0.js, 8, 10)) + + }, + /** @type {string} */ + lol, +>lol : Symbol(lol, Decl(0.js, 10, 4)) + + /** @type {number} */ + ['b' + 'ar1']: 42, +} +obj.foo = 'string' +>obj.foo : Symbol(foo, Decl(0.js, 2, 13)) +>obj : Symbol(obj, Decl(0.js, 2, 5)) +>foo : Symbol(foo, Decl(0.js, 2, 13)) + +obj.lol +>obj.lol : Symbol(lol, Decl(0.js, 10, 4)) +>obj : Symbol(obj, Decl(0.js, 2, 5)) +>lol : Symbol(lol, Decl(0.js, 10, 4)) + +obj.bar = undefined; +>obj.bar : Symbol(bar, Decl(0.js, 4, 17)) +>obj : Symbol(obj, Decl(0.js, 2, 5)) +>bar : Symbol(bar, Decl(0.js, 4, 17)) +>undefined : Symbol(undefined) + +var k = obj.method1(0); +>k : Symbol(k, Decl(0.js, 19, 3)) +>obj.method1 : Symbol(method1, Decl(0.js, 6, 12)) +>obj : Symbol(obj, Decl(0.js, 2, 5)) +>method1 : Symbol(method1, Decl(0.js, 6, 12)) + +obj.bar1 = "42"; +>obj : Symbol(obj, Decl(0.js, 2, 5)) + diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types new file mode 100644 index 0000000000000..39ae9d39c8e87 --- /dev/null +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types @@ -0,0 +1,76 @@ +=== tests/cases/conformance/jsdoc/0.js === +// @ts-check +var lol = "hello Lol" +>lol : string +>"hello Lol" : "hello Lol" + +const obj = { +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>{ /** @type {string|undefined} */ foo: undefined, /** @type {string|undefined} */ bar: "42", /** @type {function(number): number} */ method1(n1) { return n1 + 42; }, /** @type {string} */ lol, /** @type {number} */ ['b' + 'ar1']: 42,} : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } + + /** @type {string|undefined} */ + foo: undefined, +>foo : string | undefined +>undefined : undefined + + /** @type {string|undefined} */ + bar: "42", +>bar : string | undefined +>"42" : "42" + + /** @type {function(number): number} */ + method1(n1) { +>method1 : (n1: any) => any +>n1 : any + + return n1 + 42; +>n1 + 42 : any +>n1 : any +>42 : 42 + + }, + /** @type {string} */ + lol, +>lol : string + + /** @type {number} */ + ['b' + 'ar1']: 42, +>'b' + 'ar1' : string +>'b' : "b" +>'ar1' : "ar1" +>42 : 42 +} +obj.foo = 'string' +>obj.foo = 'string' : "string" +>obj.foo : string | undefined +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>foo : string | undefined +>'string' : "string" + +obj.lol +>obj.lol : string +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>lol : string + +obj.bar = undefined; +>obj.bar = undefined : undefined +>obj.bar : string | undefined +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>bar : string | undefined +>undefined : undefined + +var k = obj.method1(0); +>k : number +>obj.method1(0) : number +>obj.method1 : (arg0: number) => number +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>method1 : (arg0: number) => number +>0 : 0 + +obj.bar1 = "42"; +>obj.bar1 = "42" : "42" +>obj.bar1 : any +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>bar1 : any +>"42" : "42" + diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt new file mode 100644 index 0000000000000..007bb61ab7d0f --- /dev/null +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt @@ -0,0 +1,39 @@ +tests/cases/conformance/jsdoc/0.js(5,3): error TS2322: Type 'number' is not assignable to type 'string | undefined'. +tests/cases/conformance/jsdoc/0.js(7,3): error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/jsdoc/0.js(11,3): error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. + Type 'string' is not assignable to type 'number'. +tests/cases/conformance/jsdoc/0.js(13,3): error TS2322: Type 'undefined' is not assignable to type 'string'. +tests/cases/conformance/jsdoc/0.js(17,5): error TS2322: Type 'number' is not assignable to type 'string'. + + +==== tests/cases/conformance/jsdoc/0.js (5 errors) ==== + // @ts-check + var lol; + const obj = { + /** @type {string|undefined} */ + bar: 42, + ~~~~~~~ +!!! error TS2322: Type 'number' is not assignable to type 'string | undefined'. + /** @type {function(number): number} */ + method1(n1) { + ~~~~~~~ +!!! error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. + return "42"; + }, + /** @type {function(number): number} */ + method2: (n1) => "lol", + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. +!!! error TS2322: Type 'string' is not assignable to type 'number'. + /** @type {string} */ + lol + ~~~ +!!! error TS2322: Type 'undefined' is not assignable to type 'string'. + } + lol = "string" + /** @type {string} */ + var s = obj.method1(0); + ~ +!!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js new file mode 100644 index 0000000000000..ee2a2b76dcd5b --- /dev/null +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js @@ -0,0 +1,37 @@ +//// [0.js] +// @ts-check +var lol; +const obj = { + /** @type {string|undefined} */ + bar: 42, + /** @type {function(number): number} */ + method1(n1) { + return "42"; + }, + /** @type {function(number): number} */ + method2: (n1) => "lol", + /** @type {string} */ + lol +} +lol = "string" +/** @type {string} */ +var s = obj.method1(0); + +//// [0.js] +// @ts-check +var lol; +var obj = { + /** @type {string|undefined} */ + bar: 42, + /** @type {function(number): number} */ + method1: function (n1) { + return "42"; + }, + /** @type {function(number): number} */ + method2: function (n1) { return "lol"; }, + /** @type {string} */ + lol: lol +}; +lol = "string"; +/** @type {string} */ +var s = obj.method1(0); diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts index b45f3f0cc5188..a0f98b1cd59b8 100644 --- a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts @@ -9,16 +9,18 @@ const obj = { /** @type {string|undefined} */ foo: undefined, /** @type {string|undefined} */ - bar: 42, + bar: "42", /** @type {function(number): number} */ method1(n1) { return n1 + 42; }, /** @type {string} */ - lol + lol, + /** @type {number} */ + ['b' + 'ar1']: 42, } obj.foo = 'string' -obj.foo; obj.lol obj.bar = undefined; -var k = obj.method1(0); \ No newline at end of file +var k = obj.method1(0); +obj.bar1 = "42"; \ No newline at end of file diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts index 262858381374c..3ef19f73d7f73 100644 --- a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts @@ -4,17 +4,19 @@ // @filename: 0.js // @ts-check +var lol; const obj = { - /** @type {string|undefined} */ - foo: undefined, /** @type {string|undefined} */ bar: 42, /** @type {function(number): number} */ method1(n1) { - return (n1 + 42).toString() + return "42"; }, + /** @type {function(number): number} */ + method2: (n1) => "lol", /** @type {string} */ lol } -var lol = "string" -obj.foo = 5 +lol = "string" +/** @type {string} */ +var s = obj.method1(0); \ No newline at end of file From d392f1edabce9cef0550af8a5d50eb2237189382 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 13 Jun 2017 14:09:16 -0700 Subject: [PATCH 4/6] Remove unnecessary get type from JSDoc comment --- src/compiler/checker.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a3f3eee44b760..83286caf78224 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12748,12 +12748,6 @@ namespace ts { if (typeNode) { return getTypeFromTypeNode(typeNode); } - if (isInJavaScriptFile(declaration)) { - const jsDocType = getTypeForDeclarationFromJSDocComment(declaration); - if (jsDocType) { - return jsDocType; - } - } if (declaration.kind === SyntaxKind.Parameter) { const type = getContextuallyTypedParameterType(declaration); if (type) { From 3062d36463cf4777932274e5633755fe8aaee536 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 13 Jun 2017 15:20:52 -0700 Subject: [PATCH 5/6] Add "undefined" as return type --- src/compiler/utilities.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3833fc3087544..fde03cc7db225 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2721,11 +2721,11 @@ namespace ts { * Gets the effective type annotation of a variable, parameter, or property. If the node was * parsed in a JavaScript file, gets the type annotation from JSDoc. */ - export function getEffectiveTypeAnnotationNode(node: VariableLikeDeclaration): TypeNode { + export function getEffectiveTypeAnnotationNode(node: VariableLikeDeclaration): TypeNode | undefined { if (node.type) { return node.type; } - if (node.flags & NodeFlags.JavaScriptFile) { + if (isInJavaScriptFile(node)) { return getJSDocType(node); } } @@ -2734,11 +2734,11 @@ namespace ts { * Gets the effective return type annotation of a signature. If the node was parsed in a * JavaScript file, gets the return type annotation from JSDoc. */ - export function getEffectiveReturnTypeNode(node: SignatureDeclaration): TypeNode { + export function getEffectiveReturnTypeNode(node: SignatureDeclaration): TypeNode | undefined { if (node.type) { return node.type; } - if (node.flags & NodeFlags.JavaScriptFile) { + if (isInJavaScriptFile(node)) { return getJSDocReturnType(node); } } From 9df2931aa3b8b7715cbb5410f46fdb3cf7ba3a63 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Tue, 13 Jun 2017 15:24:05 -0700 Subject: [PATCH 6/6] Add tests and update baselines --- .../checkJsdocTypeTagOnObjectProperty1.js | 8 ++++- ...checkJsdocTypeTagOnObjectProperty1.symbols | 12 +++++++- .../checkJsdocTypeTagOnObjectProperty1.types | 30 ++++++++++++++----- ...ckJsdocTypeTagOnObjectProperty2.errors.txt | 23 +++++++++++--- .../checkJsdocTypeTagOnObjectProperty2.js | 14 ++++++++- .../checkJsdocTypeTagOnObjectProperty1.ts | 5 +++- .../checkJsdocTypeTagOnObjectProperty2.ts | 7 ++++- 7 files changed, 83 insertions(+), 16 deletions(-) diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js index 79f5d7bfd9aa9..26f1981585534 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.js @@ -14,12 +14,15 @@ const obj = { lol, /** @type {number} */ ['b' + 'ar1']: 42, + /** @type {function(number): number} */ + arrowFunc: (num) => num + 42 } obj.foo = 'string' obj.lol obj.bar = undefined; var k = obj.method1(0); -obj.bar1 = "42"; +obj.bar1 = "42"; +obj.arrowFunc(0); //// [0.js] // @ts-check @@ -38,10 +41,13 @@ var obj = (_a = { }, /** @type {number} */ _a['b' + 'ar1'] = 42, + /** @type {function(number): number} */ + _a.arrowFunc = function (num) { return num + 42; }, _a); obj.foo = 'string'; obj.lol; obj.bar = undefined; var k = obj.method1(0); obj.bar1 = "42"; +obj.arrowFunc(0); var _a; diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols index 9a1b1acb8a898..27f70798c7118 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.symbols @@ -30,6 +30,11 @@ const obj = { /** @type {number} */ ['b' + 'ar1']: 42, + /** @type {function(number): number} */ + arrowFunc: (num) => num + 42 +>arrowFunc : Symbol(arrowFunc, Decl(0.js, 14, 20)) +>num : Symbol(num, Decl(0.js, 16, 14)) +>num : Symbol(num, Decl(0.js, 16, 14)) } obj.foo = 'string' >obj.foo : Symbol(foo, Decl(0.js, 2, 13)) @@ -48,7 +53,7 @@ obj.bar = undefined; >undefined : Symbol(undefined) var k = obj.method1(0); ->k : Symbol(k, Decl(0.js, 19, 3)) +>k : Symbol(k, Decl(0.js, 21, 3)) >obj.method1 : Symbol(method1, Decl(0.js, 6, 12)) >obj : Symbol(obj, Decl(0.js, 2, 5)) >method1 : Symbol(method1, Decl(0.js, 6, 12)) @@ -56,3 +61,8 @@ var k = obj.method1(0); obj.bar1 = "42"; >obj : Symbol(obj, Decl(0.js, 2, 5)) +obj.arrowFunc(0); +>obj.arrowFunc : Symbol(arrowFunc, Decl(0.js, 14, 20)) +>obj : Symbol(obj, Decl(0.js, 2, 5)) +>arrowFunc : Symbol(arrowFunc, Decl(0.js, 14, 20)) + diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types index 39ae9d39c8e87..72c5c55228e0d 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty1.types @@ -5,8 +5,8 @@ var lol = "hello Lol" >"hello Lol" : "hello Lol" const obj = { ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } ->{ /** @type {string|undefined} */ foo: undefined, /** @type {string|undefined} */ bar: "42", /** @type {function(number): number} */ method1(n1) { return n1 + 42; }, /** @type {string} */ lol, /** @type {number} */ ['b' + 'ar1']: 42,} : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } +>{ /** @type {string|undefined} */ foo: undefined, /** @type {string|undefined} */ bar: "42", /** @type {function(number): number} */ method1(n1) { return n1 + 42; }, /** @type {string} */ lol, /** @type {number} */ ['b' + 'ar1']: 42, /** @type {function(number): number} */ arrowFunc: (num) => num + 42} : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } /** @type {string|undefined} */ foo: undefined, @@ -38,24 +38,33 @@ const obj = { >'b' + 'ar1' : string >'b' : "b" >'ar1' : "ar1" +>42 : 42 + + /** @type {function(number): number} */ + arrowFunc: (num) => num + 42 +>arrowFunc : (arg0: number) => number +>(num) => num + 42 : (num: any) => any +>num : any +>num + 42 : any +>num : any >42 : 42 } obj.foo = 'string' >obj.foo = 'string' : "string" >obj.foo : string | undefined ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } >foo : string | undefined >'string' : "string" obj.lol >obj.lol : string ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } >lol : string obj.bar = undefined; >obj.bar = undefined : undefined >obj.bar : string | undefined ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } >bar : string | undefined >undefined : undefined @@ -63,14 +72,21 @@ var k = obj.method1(0); >k : number >obj.method1(0) : number >obj.method1 : (arg0: number) => number ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } >method1 : (arg0: number) => number >0 : 0 obj.bar1 = "42"; >obj.bar1 = "42" : "42" >obj.bar1 : any ->obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; } +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } >bar1 : any >"42" : "42" +obj.arrowFunc(0); +>obj.arrowFunc(0) : number +>obj.arrowFunc : (arg0: number) => number +>obj : { [x: string]: any; foo: string | undefined; bar: string | undefined; method1(arg0: number): number; lol: string; arrowFunc: (arg0: number) => number; } +>arrowFunc : (arg0: number) => number +>0 : 0 + diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt index 007bb61ab7d0f..33c83e6117236 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.errors.txt @@ -3,11 +3,15 @@ tests/cases/conformance/jsdoc/0.js(7,3): error TS2322: Type '(n1: any) => string Type 'string' is not assignable to type 'number'. tests/cases/conformance/jsdoc/0.js(11,3): error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. Type 'string' is not assignable to type 'number'. -tests/cases/conformance/jsdoc/0.js(13,3): error TS2322: Type 'undefined' is not assignable to type 'string'. -tests/cases/conformance/jsdoc/0.js(17,5): error TS2322: Type 'number' is not assignable to type 'string'. +tests/cases/conformance/jsdoc/0.js(13,3): error TS2322: Type '(num?: string) => string' is not assignable to type '(arg0: number) => number'. + Types of parameters 'num' and 'arg0' are incompatible. + Type 'number' is not assignable to type 'string | undefined'. +tests/cases/conformance/jsdoc/0.js(15,3): error TS2322: Type 'undefined' is not assignable to type 'string'. +tests/cases/conformance/jsdoc/0.js(19,5): error TS2322: Type 'number' is not assignable to type 'string'. +tests/cases/conformance/jsdoc/0.js(22,22): error TS2345: Argument of type '"0"' is not assignable to parameter of type 'number'. -==== tests/cases/conformance/jsdoc/0.js (5 errors) ==== +==== tests/cases/conformance/jsdoc/0.js (7 errors) ==== // @ts-check var lol; const obj = { @@ -27,6 +31,12 @@ tests/cases/conformance/jsdoc/0.js(17,5): error TS2322: Type 'number' is not ass ~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type '(n1: any) => string' is not assignable to type '(arg0: number) => number'. !!! error TS2322: Type 'string' is not assignable to type 'number'. + /** @type {function(number): number} */ + arrowFunc: (num="0") => num + 42, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '(num?: string) => string' is not assignable to type '(arg0: number) => number'. +!!! error TS2322: Types of parameters 'num' and 'arg0' are incompatible. +!!! error TS2322: Type 'number' is not assignable to type 'string | undefined'. /** @type {string} */ lol ~~~ @@ -36,4 +46,9 @@ tests/cases/conformance/jsdoc/0.js(17,5): error TS2322: Type 'number' is not ass /** @type {string} */ var s = obj.method1(0); ~ -!!! error TS2322: Type 'number' is not assignable to type 'string'. \ No newline at end of file +!!! error TS2322: Type 'number' is not assignable to type 'string'. + + /** @type {string} */ + var s1 = obj.method2("0"); + ~~~ +!!! error TS2345: Argument of type '"0"' is not assignable to parameter of type 'number'. \ No newline at end of file diff --git a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js index ee2a2b76dcd5b..ab574fddcedbf 100644 --- a/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js +++ b/tests/baselines/reference/checkJsdocTypeTagOnObjectProperty2.js @@ -10,12 +10,17 @@ const obj = { }, /** @type {function(number): number} */ method2: (n1) => "lol", + /** @type {function(number): number} */ + arrowFunc: (num="0") => num + 42, /** @type {string} */ lol } lol = "string" /** @type {string} */ -var s = obj.method1(0); +var s = obj.method1(0); + +/** @type {string} */ +var s1 = obj.method2("0"); //// [0.js] // @ts-check @@ -29,9 +34,16 @@ var obj = { }, /** @type {function(number): number} */ method2: function (n1) { return "lol"; }, + /** @type {function(number): number} */ + arrowFunc: function (num) { + if (num === void 0) { num = "0"; } + return num + 42; + }, /** @type {string} */ lol: lol }; lol = "string"; /** @type {string} */ var s = obj.method1(0); +/** @type {string} */ +var s1 = obj.method2("0"); diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts index a0f98b1cd59b8..a98b0a70a2af5 100644 --- a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty1.ts @@ -18,9 +18,12 @@ const obj = { lol, /** @type {number} */ ['b' + 'ar1']: 42, + /** @type {function(number): number} */ + arrowFunc: (num) => num + 42 } obj.foo = 'string' obj.lol obj.bar = undefined; var k = obj.method1(0); -obj.bar1 = "42"; \ No newline at end of file +obj.bar1 = "42"; +obj.arrowFunc(0); \ No newline at end of file diff --git a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts index 3ef19f73d7f73..9bab96dee9b26 100644 --- a/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts +++ b/tests/cases/conformance/jsdoc/checkJsdocTypeTagOnObjectProperty2.ts @@ -14,9 +14,14 @@ const obj = { }, /** @type {function(number): number} */ method2: (n1) => "lol", + /** @type {function(number): number} */ + arrowFunc: (num="0") => num + 42, /** @type {string} */ lol } lol = "string" /** @type {string} */ -var s = obj.method1(0); \ No newline at end of file +var s = obj.method1(0); + +/** @type {string} */ +var s1 = obj.method2("0"); \ No newline at end of file