Skip to content

Commit 75556a6

Browse files
Extract types from object literals instead of using the type checker.
1 parent b3c67d3 commit 75556a6

File tree

161 files changed

+576
-438
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+576
-438
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6154,15 +6154,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
61546154
const type = instantiateType(getWidenedType(getRegularTypeOfExpression(expr)), context.mapper);
61556155
return typeToTypeNodeHelper(type, context);
61566156
},
6157-
serializeTypeOfDeclaration(syntacticContext, declaration, symbol) {
6157+
serializeTypeOfDeclaration(syntacticContext, declaration, symbol, widen) {
61586158
// Get type of the symbol if this is the valid symbol otherwise get type at location
61596159
const context = syntacticContext as NodeBuilderContext;
61606160
symbol ??= getSymbolOfDeclaration(declaration);
61616161
let type = context.enclosingSymbolTypes?.get(getSymbolId(symbol));
61626162
if (type === undefined) {
6163-
type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
6164-
? instantiateType(getWidenedLiteralType(getTypeOfSymbol(symbol)), context.mapper)
6165-
: errorType;
6163+
const symbolType = getTypeOfSymbol(symbol);
6164+
if (widen) {
6165+
type = instantiateType(getWidenedType(symbolType), context.mapper);
6166+
}
6167+
else {
6168+
type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
6169+
? instantiateType(getWidenedLiteralType(symbolType), context.mapper)
6170+
: errorType;
6171+
}
61666172
}
61676173
const addUndefinedForParameter = declaration && (isParameter(declaration) || isJSDocParameterTag(declaration)) && requiresAddingImplicitUndefined(declaration, context.enclosingDeclaration);
61686174
if (addUndefinedForParameter) {

src/compiler/expressionToTypeNode.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,15 @@ export function createSyntacticTypeNodeBuilder(
834834
symbol: Symbol | undefined,
835835
context: SyntacticTypeNodeBuilderContext,
836836
reportFallback = true,
837+
widen = false,
837838
) {
838839
if (reportFallback) {
839840
context.tracker.reportInferenceFallback(node);
840841
}
841842
if (context.noInferenceFallback === true) {
842843
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
843844
}
844-
return resolver.serializeTypeOfDeclaration(context, node, symbol);
845+
return resolver.serializeTypeOfDeclaration(context, node, symbol, widen);
845846
}
846847

847848
function inferExpressionType(node: Expression, context: SyntacticTypeNodeBuilderContext, reportFallback = true, requiresAddingUndefined?: boolean) {
@@ -1058,9 +1059,6 @@ export function createSyntacticTypeNodeBuilder(
10581059
}
10591060
return syntacticResult(inferExpressionType(objectLiteral, context, /*reportFallback*/ false, requiresAddingUndefined));
10601061
}
1061-
// Disable any inference fallback since we won't actually use the resulting type and we don't want to generate errors
1062-
const oldNoInferenceFallback = context.noInferenceFallback;
1063-
context.noInferenceFallback = true;
10641062
const properties: TypeElement[] = [];
10651063
const oldFlags = context.flags;
10661064
context.flags |= NodeBuilderFlags.InObjectTypeLiteral;
@@ -1092,16 +1090,15 @@ export function createSyntacticTypeNodeBuilder(
10921090
if (!(context.flags & NodeBuilderFlags.MultilineObjectLiterals)) {
10931091
setEmitFlags(typeNode, EmitFlags.SingleLine);
10941092
}
1095-
context.noInferenceFallback = oldNoInferenceFallback;
1096-
return notImplemented;
1093+
return syntacticResult(addUndefinedIfNeeded(typeNode, requiresAddingUndefined, objectLiteral, context));
10971094
}
10981095

10991096
function typeFromObjectLiteralPropertyAssignment(prop: PropertyAssignment, name: PropertyName, context: SyntacticTypeNodeBuilderContext, isConstContext: boolean) {
11001097
const modifiers = isConstContext ?
11011098
[factory.createModifier(SyntaxKind.ReadonlyKeyword)] :
11021099
[];
11031100
const expressionResult = typeFromExpression(prop.initializer, context, isConstContext);
1104-
const typeNode = expressionResult.type !== undefined ? expressionResult.type : inferTypeOfDeclaration(prop, /*symbol*/ undefined, context, expressionResult.reportFallback);
1101+
const typeNode = expressionResult.type !== undefined ? expressionResult.type : inferTypeOfDeclaration(prop, /*symbol*/ undefined, context, expressionResult.reportFallback, !isConstContext);
11051102

11061103
return factory.createPropertySignature(
11071104
modifiers,

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10556,7 +10556,7 @@ export interface SyntacticTypeNodeBuilderResolver {
1055610556
serializeExistingTypeNode(context: SyntacticTypeNodeBuilderContext, node: TypeNode, addUndefined?: boolean): TypeNode | undefined;
1055710557
serializeReturnTypeForSignature(context: SyntacticTypeNodeBuilderContext, signatureDeclaration: SignatureDeclaration | JSDocSignature): TypeNode | undefined;
1055810558
serializeTypeOfExpression(context: SyntacticTypeNodeBuilderContext, expr: Expression): TypeNode;
10559-
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined): TypeNode | undefined;
10559+
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined, widen?: boolean): TypeNode | undefined;
1056010560
serializeNameOfParameter(context: SyntacticTypeNodeBuilderContext, parameter: ParameterDeclaration): BindingName | string;
1056110561
serializeTypeName(context: SyntacticTypeNodeBuilderContext, node: EntityName, isTypeOf?: boolean, typeArguments?: readonly TypeNode[]): TypeNode | undefined;
1056210562
serializeEntityName(context: SyntacticTypeNodeBuilderContext, node: EntityNameExpression): Expression | undefined;

tests/baselines/reference/ES5For-of10.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== ES5For-of10.ts ===
44
function foo() {
55
>foo : () => { x: number; }
6-
> : ^^^^^^^^^^^^^^^^^^^^
6+
> : ^^^^^^^^ ^^^^^^^^^^^
77

88
return { x: 0 };
99
>{ x: 0 } : { x: number; }
@@ -19,7 +19,7 @@ for (foo().x of []) {
1919
>foo() : { x: number; }
2020
> : ^^^^^^^^^^^^^^
2121
>foo : () => { x: number; }
22-
> : ^^^^^^^^^^^^^^^^^^^^
22+
> : ^^^^^^^^ ^^^^^^^^^^^
2323
>x : number
2424
> : ^^^^^^
2525
>[] : undefined[]
@@ -31,7 +31,7 @@ for (foo().x of []) {
3131
>foo() : { x: number; }
3232
> : ^^^^^^^^^^^^^^
3333
>foo : () => { x: number; }
34-
> : ^^^^^^^^^^^^^^^^^^^^
34+
> : ^^^^^^^^ ^^^^^^^^^^^
3535
>x : number
3636
> : ^^^^^^
3737
>[] : undefined[]
@@ -45,7 +45,7 @@ for (foo().x of []) {
4545
>foo() : { x: number; }
4646
> : ^^^^^^^^^^^^^^
4747
>foo : () => { x: number; }
48-
> : ^^^^^^^^^^^^^^^^^^^^
48+
> : ^^^^^^^^ ^^^^^^^^^^^
4949
>x : number
5050
> : ^^^^^^
5151
}

tests/baselines/reference/ES5For-of34.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== ES5For-of34.ts ===
44
function foo() {
55
>foo : () => { x: number; }
6-
> : ^^^^^^^^^^^^^^^^^^^^
6+
> : ^^^^^^^^ ^^^^^^^^^^^
77

88
return { x: 0 };
99
>{ x: 0 } : { x: number; }
@@ -19,7 +19,7 @@ for (foo().x of ['a', 'b', 'c']) {
1919
>foo() : { x: number; }
2020
> : ^^^^^^^^^^^^^^
2121
>foo : () => { x: number; }
22-
> : ^^^^^^^^^^^^^^^^^^^^
22+
> : ^^^^^^^^ ^^^^^^^^^^^
2323
>x : number
2424
> : ^^^^^^
2525
>['a', 'b', 'c'] : string[]
@@ -39,7 +39,7 @@ for (foo().x of ['a', 'b', 'c']) {
3939
>foo() : { x: number; }
4040
> : ^^^^^^^^^^^^^^
4141
>foo : () => { x: number; }
42-
> : ^^^^^^^^^^^^^^^^^^^^
42+
> : ^^^^^^^^ ^^^^^^^^^^^
4343
>x : number
4444
> : ^^^^^^
4545
}

tests/baselines/reference/ES5For-of8.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== ES5For-of8.ts ===
44
function foo() {
55
>foo : () => { x: number; }
6-
> : ^^^^^^^^^^^^^^^^^^^^
6+
> : ^^^^^^^^ ^^^^^^^^^^^
77

88
return { x: 0 };
99
>{ x: 0 } : { x: number; }
@@ -19,7 +19,7 @@ for (foo().x of ['a', 'b', 'c']) {
1919
>foo() : { x: number; }
2020
> : ^^^^^^^^^^^^^^
2121
>foo : () => { x: number; }
22-
> : ^^^^^^^^^^^^^^^^^^^^
22+
> : ^^^^^^^^ ^^^^^^^^^^^
2323
>x : number
2424
> : ^^^^^^
2525
>['a', 'b', 'c'] : string[]
@@ -39,7 +39,7 @@ for (foo().x of ['a', 'b', 'c']) {
3939
>foo() : { x: number; }
4040
> : ^^^^^^^^^^^^^^
4141
>foo : () => { x: number; }
42-
> : ^^^^^^^^^^^^^^^^^^^^
42+
> : ^^^^^^^^ ^^^^^^^^^^^
4343
>x : number
4444
> : ^^^^^^
4545
}

tests/baselines/reference/ES5For-of9.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
=== ES5For-of9.ts ===
44
function foo() {
55
>foo : () => { x: number; }
6-
> : ^^^^^^^^^^^^^^^^^^^^
6+
> : ^^^^^^^^ ^^^^^^^^^^^
77

88
return { x: 0 };
99
>{ x: 0 } : { x: number; }
@@ -19,7 +19,7 @@ for (foo().x of []) {
1919
>foo() : { x: number; }
2020
> : ^^^^^^^^^^^^^^
2121
>foo : () => { x: number; }
22-
> : ^^^^^^^^^^^^^^^^^^^^
22+
> : ^^^^^^^^ ^^^^^^^^^^^
2323
>x : number
2424
> : ^^^^^^
2525
>[] : undefined[]
@@ -31,7 +31,7 @@ for (foo().x of []) {
3131
>foo() : { x: number; }
3232
> : ^^^^^^^^^^^^^^
3333
>foo : () => { x: number; }
34-
> : ^^^^^^^^^^^^^^^^^^^^
34+
> : ^^^^^^^^ ^^^^^^^^^^^
3535
>x : number
3636
> : ^^^^^^
3737
>[] : undefined[]
@@ -45,7 +45,7 @@ for (foo().x of []) {
4545
>foo() : { x: number; }
4646
> : ^^^^^^^^^^^^^^
4747
>foo : () => { x: number; }
48-
> : ^^^^^^^^^^^^^^^^^^^^
48+
> : ^^^^^^^^ ^^^^^^^^^^^
4949
>x : number
5050
> : ^^^^^^
5151
}

tests/baselines/reference/ES5For-ofTypeCheck10.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MyStringIterator {
88

99
next() {
1010
>next : () => { done: boolean; value: string; }
11-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
> : ^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^
1212

1313
return {
1414
>{ done: true, value: "" } : { done: boolean; value: string; }

tests/baselines/reference/FunctionAndModuleWithSameNameAndDifferentCommonRoot.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module A {
77

88
export function Point() {
99
>Point : () => { x: number; y: number; }
10-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
1111

1212
return { x: 0, y: 0 };
1313
>{ x: 0, y: 0 } : { x: number; y: number; }
@@ -61,11 +61,11 @@ var fn = A.Point;
6161
>fn : () => { x: number; y: number; }
6262
> : ^^^^^^
6363
>A.Point : () => { x: number; y: number; }
64-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
6565
>A : typeof A
6666
> : ^^^^^^^^
6767
>Point : () => { x: number; y: number; }
68-
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
6969

7070
var cl: { x: number; y: number; }
7171
>cl : { x: number; y: number; }

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedAndNonExportedClassesOfTheSameName.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module A {
2929

3030
fromCarthesian(p: A.Point) {
3131
>fromCarthesian : (p: A.Point) => { x: number; y: number; }
32-
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
> : ^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
3333
>p : A.Point
3434
> : ^^^^^^^
3535
>A : any

0 commit comments

Comments
 (0)