Skip to content

Commit 9ba0800

Browse files
Andaristsandersn
andauthored
Fixed braceless type tags with types starting with an open parenthesis (microsoft#57167)
Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
1 parent bdd1f94 commit 9ba0800

File tree

7 files changed

+167
-1
lines changed

7 files changed

+167
-1
lines changed

src/compiler/scanner.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,6 +2656,10 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
26562656
return token = SyntaxKind.OpenBracketToken;
26572657
case CharacterCodes.closeBracket:
26582658
return token = SyntaxKind.CloseBracketToken;
2659+
case CharacterCodes.openParen:
2660+
return token = SyntaxKind.OpenParenToken;
2661+
case CharacterCodes.closeParen:
2662+
return token = SyntaxKind.CloseParenToken;
26592663
case CharacterCodes.lessThan:
26602664
return token = SyntaxKind.LessThanToken;
26612665
case CharacterCodes.greaterThan:

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ export type JSDocSyntaxKind =
764764
| SyntaxKind.GreaterThanToken
765765
| SyntaxKind.OpenBracketToken
766766
| SyntaxKind.CloseBracketToken
767+
| SyntaxKind.OpenParenToken
768+
| SyntaxKind.CloseParenToken
767769
| SyntaxKind.EqualsToken
768770
| SyntaxKind.CommaToken
769771
| SyntaxKind.DotToken

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4134,7 +4134,7 @@ declare namespace ts {
41344134
type KeywordTypeSyntaxKind = SyntaxKind.AnyKeyword | SyntaxKind.BigIntKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.IntrinsicKeyword | SyntaxKind.NeverKeyword | SyntaxKind.NumberKeyword | SyntaxKind.ObjectKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.UnknownKeyword | SyntaxKind.VoidKeyword;
41354135
type TokenSyntaxKind = SyntaxKind.Unknown | SyntaxKind.EndOfFileToken | TriviaSyntaxKind | LiteralSyntaxKind | PseudoLiteralSyntaxKind | PunctuationSyntaxKind | SyntaxKind.Identifier | KeywordSyntaxKind;
41364136
type JsxTokenSyntaxKind = SyntaxKind.LessThanSlashToken | SyntaxKind.EndOfFileToken | SyntaxKind.ConflictMarkerTrivia | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.OpenBraceToken | SyntaxKind.LessThanToken;
4137-
type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.HashToken | SyntaxKind.Unknown | KeywordSyntaxKind;
4137+
type JSDocSyntaxKind = SyntaxKind.EndOfFileToken | SyntaxKind.WhitespaceTrivia | SyntaxKind.AtToken | SyntaxKind.NewLineTrivia | SyntaxKind.AsteriskToken | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.OpenParenToken | SyntaxKind.CloseParenToken | SyntaxKind.EqualsToken | SyntaxKind.CommaToken | SyntaxKind.DotToken | SyntaxKind.Identifier | SyntaxKind.BacktickToken | SyntaxKind.HashToken | SyntaxKind.Unknown | KeywordSyntaxKind;
41384138
enum NodeFlags {
41394139
None = 0,
41404140
Let = 1,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
index.js(3,3): error TS2322: Type 'number' is not assignable to type 'string'.
2+
index.js(20,16): error TS2322: Type '"other"' is not assignable to type '"foo" | "bar"'.
3+
4+
5+
==== index.js (2 errors) ====
6+
/** @type () => string */
7+
function fn1() {
8+
return 42;
9+
~~~~~~
10+
!!! error TS2322: Type 'number' is not assignable to type 'string'.
11+
}
12+
13+
/** @type () => string */
14+
function fn2() {
15+
return "foo";
16+
}
17+
18+
/** @type (arg: string) => string */
19+
function fn3(arg) {
20+
return arg;
21+
}
22+
23+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
24+
const obj1 = { type: "foo", prop: 10 };
25+
26+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
27+
const obj2 = { type: "other", prop: 10 };
28+
~~~~
29+
!!! error TS2322: Type '"other"' is not assignable to type '"foo" | "bar"'.
30+
!!! related TS6500 index.js:19:14: The expected type comes from property 'type' which is declared here on type '({ type: "foo"; } | { type: "bar"; }) & { prop: number; }'
31+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [tests/cases/compiler/jsdocBracelessTypeTag1.ts] ////
2+
3+
=== index.js ===
4+
/** @type () => string */
5+
function fn1() {
6+
>fn1 : Symbol(fn1, Decl(index.js, 0, 0))
7+
8+
return 42;
9+
}
10+
11+
/** @type () => string */
12+
function fn2() {
13+
>fn2 : Symbol(fn2, Decl(index.js, 3, 1))
14+
15+
return "foo";
16+
}
17+
18+
/** @type (arg: string) => string */
19+
function fn3(arg) {
20+
>fn3 : Symbol(fn3, Decl(index.js, 8, 1))
21+
>arg : Symbol(arg, Decl(index.js, 11, 13))
22+
23+
return arg;
24+
>arg : Symbol(arg, Decl(index.js, 11, 13))
25+
}
26+
27+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
28+
const obj1 = { type: "foo", prop: 10 };
29+
>obj1 : Symbol(obj1, Decl(index.js, 16, 5))
30+
>type : Symbol(type, Decl(index.js, 16, 14))
31+
>prop : Symbol(prop, Decl(index.js, 16, 27))
32+
33+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
34+
const obj2 = { type: "other", prop: 10 };
35+
>obj2 : Symbol(obj2, Decl(index.js, 19, 5))
36+
>type : Symbol(type, Decl(index.js, 19, 14))
37+
>prop : Symbol(prop, Decl(index.js, 19, 29))
38+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//// [tests/cases/compiler/jsdocBracelessTypeTag1.ts] ////
2+
3+
=== index.js ===
4+
/** @type () => string */
5+
function fn1() {
6+
>fn1 : () => string
7+
> : ^^^^^^
8+
9+
return 42;
10+
>42 : 42
11+
> : ^^
12+
}
13+
14+
/** @type () => string */
15+
function fn2() {
16+
>fn2 : () => string
17+
> : ^^^^^^
18+
19+
return "foo";
20+
>"foo" : "foo"
21+
> : ^^^^^
22+
}
23+
24+
/** @type (arg: string) => string */
25+
function fn3(arg) {
26+
>fn3 : (arg: string) => string
27+
> : ^ ^^ ^^^^^
28+
>arg : string
29+
> : ^^^^^^
30+
31+
return arg;
32+
>arg : string
33+
> : ^^^^^^
34+
}
35+
36+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
37+
const obj1 = { type: "foo", prop: 10 };
38+
>obj1 : ({ type: "foo"; } | { type: "bar"; }) & { prop: number; }
39+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
>{ type: "foo", prop: 10 } : { type: "foo"; prop: number; }
41+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
>type : "foo"
43+
> : ^^^^^
44+
>"foo" : "foo"
45+
> : ^^^^^
46+
>prop : number
47+
> : ^^^^^^
48+
>10 : 10
49+
> : ^^
50+
51+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
52+
const obj2 = { type: "other", prop: 10 };
53+
>obj2 : ({ type: "foo"; } | { type: "bar"; }) & { prop: number; }
54+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
>{ type: "other", prop: 10 } : { type: "other"; prop: number; }
56+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57+
>type : "other"
58+
> : ^^^^^^^
59+
>"other" : "other"
60+
> : ^^^^^^^
61+
>prop : number
62+
> : ^^^^^^
63+
>10 : 10
64+
> : ^^
65+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @strict: true
2+
// @noEmit: true
3+
// @allowJs: true
4+
// @checkJs: true
5+
// @filename: index.js
6+
7+
/** @type () => string */
8+
function fn1() {
9+
return 42;
10+
}
11+
12+
/** @type () => string */
13+
function fn2() {
14+
return "foo";
15+
}
16+
17+
/** @type (arg: string) => string */
18+
function fn3(arg) {
19+
return arg;
20+
}
21+
22+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
23+
const obj1 = { type: "foo", prop: 10 };
24+
25+
/** @type ({ type: 'foo' } | { type: 'bar' }) & { prop: number } */
26+
const obj2 = { type: "other", prop: 10 };

0 commit comments

Comments
 (0)