Skip to content

Commit d4708dc

Browse files
committed
Merge branch 'master' into recursive-type-reference-cache
2 parents 520d7ff + 1edecac commit d4708dc

14 files changed

+98
-111
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9436,21 +9436,20 @@ namespace ts {
94369436
if (relation === identityRelation) {
94379437
return propertiesIdenticalTo(source, target);
94389438
}
9439+
const requireOptionalProperties = relation === subtypeRelation && !(getObjectFlags(source) & ObjectFlags.ObjectLiteral);
9440+
const unmatchedProperty = getUnmatchedProperty(source, target, requireOptionalProperties);
9441+
if (unmatchedProperty) {
9442+
if (reportErrors) {
9443+
reportError(Diagnostics.Property_0_is_missing_in_type_1, symbolToString(unmatchedProperty), typeToString(source));
9444+
}
9445+
return Ternary.False;
9446+
}
94399447
let result = Ternary.True;
94409448
const properties = getPropertiesOfObjectType(target);
9441-
const requireOptionalProperties = relation === subtypeRelation && !(getObjectFlags(source) & ObjectFlags.ObjectLiteral);
94429449
for (const targetProp of properties) {
9443-
const sourceProp = getPropertyOfType(source, targetProp.escapedName);
9444-
if (sourceProp !== targetProp) {
9445-
if (!sourceProp) {
9446-
if (!(targetProp.flags & SymbolFlags.Optional) || requireOptionalProperties) {
9447-
if (reportErrors) {
9448-
reportError(Diagnostics.Property_0_is_missing_in_type_1, symbolToString(targetProp), typeToString(source));
9449-
}
9450-
return Ternary.False;
9451-
}
9452-
}
9453-
else if (!(targetProp.flags & SymbolFlags.Prototype)) {
9450+
if (!(targetProp.flags & SymbolFlags.Prototype)) {
9451+
const sourceProp = getPropertyOfType(source, targetProp.escapedName);
9452+
if (sourceProp && sourceProp !== targetProp) {
94549453
const sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp);
94559454
const targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp);
94569455
if (sourcePropFlags & ModifierFlags.Private || targetPropFlags & ModifierFlags.Private) {
@@ -10478,17 +10477,17 @@ namespace ts {
1047810477
}
1047910478
}
1048010479

10481-
function isPossiblyAssignableTo(source: Type, target: Type) {
10480+
function getUnmatchedProperty(source: Type, target: Type, requireOptionalProperties: boolean) {
1048210481
const properties = getPropertiesOfObjectType(target);
1048310482
for (const targetProp of properties) {
10484-
if (!(targetProp.flags & (SymbolFlags.Optional | SymbolFlags.Prototype))) {
10485-
const sourceProp = getPropertyOfObjectType(source, targetProp.escapedName);
10483+
if (requireOptionalProperties || !(targetProp.flags & SymbolFlags.Optional)) {
10484+
const sourceProp = getPropertyOfType(source, targetProp.escapedName);
1048610485
if (!sourceProp) {
10487-
return false;
10486+
return targetProp;
1048810487
}
1048910488
}
1049010489
}
10491-
return true;
10490+
return undefined;
1049210491
}
1049310492

1049410493
function inferTypes(inferences: InferenceInfo[], originalSource: Type, originalTarget: Type, priority: InferencePriority = 0) {
@@ -10686,7 +10685,7 @@ namespace ts {
1068610685
}
1068710686
// Infer from the members of source and target only if the two types are possibly related. We check
1068810687
// in both directions because we may be inferring for a co-variant or a contra-variant position.
10689-
if (isPossiblyAssignableTo(source, target) || isPossiblyAssignableTo(target, source)) {
10688+
if (!getUnmatchedProperty(source, target, /*requireOptionalProperties*/ false) || !getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false)) {
1069010689
inferFromProperties(source, target);
1069110690
inferFromSignatures(source, target, SignatureKind.Call);
1069210691
inferFromSignatures(source, target, SignatureKind.Construct);

src/compiler/core.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,14 +1316,12 @@ namespace ts {
13161316

13171317
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: (string | number)[]): Diagnostic;
13181318
export function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage): Diagnostic {
1319-
const end = start + length;
1320-
13211319
Debug.assertGreaterThanOrEqual(start, 0);
13221320
Debug.assertGreaterThanOrEqual(length, 0);
13231321

13241322
if (file) {
13251323
Debug.assertLessThanOrEqual(start, file.text.length);
1326-
Debug.assertLessThanOrEqual(end, file.text.length);
1324+
Debug.assertLessThanOrEqual(start + length, file.text.length);
13271325
}
13281326

13291327
let text = getLocaleSpecificMessage(message);

src/compiler/parser.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,25 +2290,27 @@ namespace ts {
22902290
signature.typeParameters = parseTypeParameters();
22912291
}
22922292
signature.parameters = parseParameterList(flags);
2293+
signature.type = parseReturnType(returnToken, !!(flags & SignatureFlags.Type));
2294+
}
22932295

2294-
const returnTokenRequired = returnToken === SyntaxKind.EqualsGreaterThanToken;
2295-
if (returnTokenRequired) {
2296+
function parseReturnType(returnToken: SyntaxKind.ColonToken | SyntaxKind.EqualsGreaterThanToken, isType: boolean): TypeNode | undefined {
2297+
return shouldParseReturnType(returnToken, isType) ? parseTypeOrTypePredicate() : undefined;
2298+
}
2299+
function shouldParseReturnType(returnToken: SyntaxKind.ColonToken | SyntaxKind.EqualsGreaterThanToken, isType: boolean): boolean {
2300+
if (returnToken === SyntaxKind.EqualsGreaterThanToken) {
22962301
parseExpected(returnToken);
2297-
signature.type = parseTypeOrTypePredicate();
2302+
return true;
22982303
}
2299-
else if (parseOptional(returnToken)) {
2300-
signature.type = parseTypeOrTypePredicate();
2304+
else if (parseOptional(SyntaxKind.ColonToken)) {
2305+
return true;
23012306
}
2302-
else if (flags & SignatureFlags.Type) {
2303-
const start = scanner.getTokenPos();
2304-
const length = scanner.getTextPos() - start;
2305-
const backwardToken = parseOptional(returnToken === SyntaxKind.ColonToken ? SyntaxKind.EqualsGreaterThanToken : SyntaxKind.ColonToken);
2306-
if (backwardToken) {
2307-
// This is easy to get backward, especially in type contexts, so parse the type anyway
2308-
signature.type = parseTypeOrTypePredicate();
2309-
parseErrorAtPosition(start, length, Diagnostics._0_expected, tokenToString(returnToken));
2310-
}
2307+
else if (isType && token() === SyntaxKind.EqualsGreaterThanToken) {
2308+
// This is easy to get backward, especially in type contexts, so parse the type anyway
2309+
parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.ColonToken));
2310+
nextToken();
2311+
return true;
23112312
}
2313+
return false;
23122314
}
23132315

23142316
function parseParameterList(flags: SignatureFlags) {
@@ -2722,6 +2724,8 @@ namespace ts {
27222724
case SyntaxKind.ObjectKeyword:
27232725
case SyntaxKind.AsteriskToken:
27242726
case SyntaxKind.QuestionToken:
2727+
case SyntaxKind.ExclamationToken:
2728+
case SyntaxKind.DotDotDotToken:
27252729
return true;
27262730
case SyntaxKind.MinusToken:
27272731
return lookAhead(nextTokenIsNumericLiteral);

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ namespace ts {
668668
name?: PropertyName;
669669
typeParameters?: NodeArray<TypeParameterDeclaration>;
670670
parameters: NodeArray<ParameterDeclaration>;
671-
type?: TypeNode;
671+
type: TypeNode | undefined;
672672
}
673673

674674
export interface CallSignatureDeclaration extends SignatureDeclaration, TypeElement {

tests/baselines/reference/arityAndOrderCompatibility01.errors.txt

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(14,12): erro
33
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,5): error TS2461: Type '{ 0: string; 1: number; }' is not an array type.
44
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(15,12): error TS2460: Type '{ 0: string; 1: number; }' has no property '2'.
55
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(16,5): error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
6-
Types of property '0' are incompatible.
7-
Type 'string' is not assignable to type 'number'.
6+
Property '2' is missing in type '[string, number]'.
87
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(17,5): error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
9-
Types of property '0' are incompatible.
10-
Type 'string' is not assignable to type 'number'.
8+
Property '2' is missing in type 'StrNum'.
119
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(18,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
12-
Types of property '0' are incompatible.
13-
Type 'string' is not assignable to type 'number'.
10+
Property '2' is missing in type '{ 0: string; 1: number; }'.
1411
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(19,5): error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
1512
Property '2' is missing in type '[string, number]'.
1613
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(20,5): error TS2322: Type 'StrNum' is not assignable to type '[string, number, number]'.
@@ -24,8 +21,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(23,5): error
2421
Types of property '0' are incompatible.
2522
Type 'string' is not assignable to type 'number'.
2623
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(24,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
27-
Types of property '0' are incompatible.
28-
Type 'string' is not assignable to type 'number'.
24+
Property 'length' is missing in type '{ 0: string; 1: number; }'.
2925
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(25,5): error TS2322: Type '[string, number]' is not assignable to type '[string]'.
3026
Types of property 'pop' are incompatible.
3127
Type '() => string | number' is not assignable to type '() => string'.
@@ -44,8 +40,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(29,5): error
4440
Types of property '0' are incompatible.
4541
Type 'string' is not assignable to type 'number'.
4642
tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
47-
Types of property '0' are incompatible.
48-
Type 'string' is not assignable to type 'number'.
43+
Property 'length' is missing in type '{ 0: string; 1: number; }'.
4944

5045

5146
==== tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts (19 errors) ====
@@ -75,18 +70,15 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
7570
var j1: [number, number, number] = x;
7671
~~
7772
!!! error TS2322: Type '[string, number]' is not assignable to type '[number, number, number]'.
78-
!!! error TS2322: Types of property '0' are incompatible.
79-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
73+
!!! error TS2322: Property '2' is missing in type '[string, number]'.
8074
var j2: [number, number, number] = y;
8175
~~
8276
!!! error TS2322: Type 'StrNum' is not assignable to type '[number, number, number]'.
83-
!!! error TS2322: Types of property '0' are incompatible.
84-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
77+
!!! error TS2322: Property '2' is missing in type 'StrNum'.
8578
var j3: [number, number, number] = z;
8679
~~
8780
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, number, number]'.
88-
!!! error TS2322: Types of property '0' are incompatible.
89-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
81+
!!! error TS2322: Property '2' is missing in type '{ 0: string; 1: number; }'.
9082
var k1: [string, number, number] = x;
9183
~~
9284
!!! error TS2322: Type '[string, number]' is not assignable to type '[string, number, number]'.
@@ -112,8 +104,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
112104
var l3: [number] = z;
113105
~~
114106
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number]'.
115-
!!! error TS2322: Types of property '0' are incompatible.
116-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
107+
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
117108
var m1: [string] = x;
118109
~~
119110
!!! error TS2322: Type '[string, number]' is not assignable to type '[string]'.
@@ -144,8 +135,7 @@ tests/cases/conformance/types/tuple/arityAndOrderCompatibility01.ts(30,5): error
144135
var n3: [number, string] = z;
145136
~~
146137
!!! error TS2322: Type '{ 0: string; 1: number; }' is not assignable to type '[number, string]'.
147-
!!! error TS2322: Types of property '0' are incompatible.
148-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
138+
!!! error TS2322: Property 'length' is missing in type '{ 0: string; 1: number; }'.
149139
var o1: [string, number] = x;
150140
var o2: [string, number] = y;
151141
var o3: [string, number] = y;

tests/baselines/reference/contextualTypeWithUnionTypeObjectLiteral.errors.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
1515
Type 'string' is not assignable to type 'number'.
1616
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(25,5): error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
1717
Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
18-
Types of property 'prop' are incompatible.
19-
Type 'string | number' is not assignable to type 'number'.
20-
Type 'string' is not assignable to type 'number'.
18+
Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }'.
2119
tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(29,5): error TS2322: Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
2220
Type '{ prop: string | number; anotherP: string; anotherP1: number; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
2321
Types of property 'prop' are incompatible.
@@ -78,9 +76,7 @@ tests/cases/conformance/types/union/contextualTypeWithUnionTypeObjectLiteral.ts(
7876
~~~~~~~~~~~~
7977
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: string; anotherP: string; } | { prop: number; anotherP1: number; }'.
8078
!!! error TS2322: Type '{ prop: string | number; anotherP: string; }' is not assignable to type '{ prop: number; anotherP1: number; }'.
81-
!!! error TS2322: Types of property 'prop' are incompatible.
82-
!!! error TS2322: Type 'string | number' is not assignable to type 'number'.
83-
!!! error TS2322: Type 'string' is not assignable to type 'number'.
79+
!!! error TS2322: Property 'anotherP1' is missing in type '{ prop: string | number; anotherP: string; }'.
8480
prop: strOrNumber,
8581
anotherP: str
8682
};

tests/baselines/reference/jsdocDisallowedInTypescript.errors.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(17,11): error TS802
1212
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(18,17): error TS8020: JSDoc types can only be used inside documentation comments.
1313
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,5): error TS2322: Type 'undefined' is not assignable to type 'number | null'.
1414
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,17): error TS8020: JSDoc types can only be used inside documentation comments.
15+
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(21,16): error TS8020: JSDoc types can only be used inside documentation comments.
16+
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(22,16): error TS8020: JSDoc types can only be used inside documentation comments.
17+
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(23,17): error TS8020: JSDoc types can only be used inside documentation comments.
18+
tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(24,17): error TS8020: JSDoc types can only be used inside documentation comments.
1519

1620

17-
==== tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts (14 errors) ====
21+
==== tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts (18 errors) ====
1822
// grammar error from checker
1923
var ara: Array.<number> = [1,2,3];
2024
~
@@ -62,4 +66,18 @@ tests/cases/conformance/jsdoc/jsdocDisallowedInTypescript.ts(19,17): error TS802
6266
!!! error TS2322: Type 'undefined' is not assignable to type 'number | null'.
6367
~~~~~~~
6468
!!! error TS8020: JSDoc types can only be used inside documentation comments.
69+
70+
var nns: Array<?number>;
71+
~~~~~~~
72+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
73+
var dns: Array<!number>;
74+
~~~~~~~
75+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
76+
var anys: Array<*>;
77+
~
78+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
79+
var vars: Array<...number>;
80+
~~~~~~~~~
81+
!!! error TS8020: JSDoc types can only be used inside documentation comments.
82+
6583

tests/baselines/reference/jsdocDisallowedInTypescript.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ var variadic: ...boolean = [true, false, true];
1818
var most: !string = 'definite';
1919
var postfixdef: number! = 101;
2020
var postfixopt: number? = undefined;
21+
22+
var nns: Array<?number>;
23+
var dns: Array<!number>;
24+
var anys: Array<*>;
25+
var vars: Array<...number>;
26+
2127

2228

2329
//// [jsdocDisallowedInTypescript.js]
@@ -40,3 +46,7 @@ var variadic = [true, false, true];
4046
var most = 'definite';
4147
var postfixdef = 101;
4248
var postfixopt = undefined;
49+
var nns;
50+
var dns;
51+
var anys;
52+
var vars;

0 commit comments

Comments
 (0)