Skip to content
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

Extract types from object literals instead of using the type checker. #60540

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6154,15 +6154,21 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const type = instantiateType(getWidenedType(getRegularTypeOfExpression(expr)), context.mapper);
return typeToTypeNodeHelper(type, context);
},
serializeTypeOfDeclaration(syntacticContext, declaration, symbol) {
serializeTypeOfDeclaration(syntacticContext, declaration, symbol, widen) {
// Get type of the symbol if this is the valid symbol otherwise get type at location
const context = syntacticContext as NodeBuilderContext;
symbol ??= getSymbolOfDeclaration(declaration);
let type = context.enclosingSymbolTypes?.get(getSymbolId(symbol));
if (type === undefined) {
type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
? instantiateType(getWidenedLiteralType(getTypeOfSymbol(symbol)), context.mapper)
: errorType;
const symbolType = getTypeOfSymbol(symbol);
if (widen) {
type = instantiateType(getWidenedType(symbolType), context.mapper);
}
else {
type = symbol && !(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.Signature))
? instantiateType(getWidenedLiteralType(symbolType), context.mapper)
: errorType;
}
jakebailey marked this conversation as resolved.
Show resolved Hide resolved
}
const addUndefinedForParameter = declaration && (isParameter(declaration) || isJSDocParameterTag(declaration)) && requiresAddingImplicitUndefined(declaration, context.enclosingDeclaration);
if (addUndefinedForParameter) {
Expand Down
11 changes: 4 additions & 7 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,15 @@ export function createSyntacticTypeNodeBuilder(
symbol: Symbol | undefined,
context: SyntacticTypeNodeBuilderContext,
reportFallback = true,
widen = false,
) {
if (reportFallback) {
context.tracker.reportInferenceFallback(node);
}
if (context.noInferenceFallback === true) {
return factory.createKeywordTypeNode(SyntaxKind.AnyKeyword);
}
return resolver.serializeTypeOfDeclaration(context, node, symbol);
return resolver.serializeTypeOfDeclaration(context, node, symbol, widen);
}

function inferExpressionType(node: Expression, context: SyntacticTypeNodeBuilderContext, reportFallback = true, requiresAddingUndefined?: boolean) {
Expand Down Expand Up @@ -1058,9 +1059,6 @@ export function createSyntacticTypeNodeBuilder(
}
return syntacticResult(inferExpressionType(objectLiteral, context, /*reportFallback*/ false, requiresAddingUndefined));
}
// Disable any inference fallback since we won't actually use the resulting type and we don't want to generate errors
const oldNoInferenceFallback = context.noInferenceFallback;
context.noInferenceFallback = true;
const properties: TypeElement[] = [];
const oldFlags = context.flags;
context.flags |= NodeBuilderFlags.InObjectTypeLiteral;
Expand Down Expand Up @@ -1092,16 +1090,15 @@ export function createSyntacticTypeNodeBuilder(
if (!(context.flags & NodeBuilderFlags.MultilineObjectLiterals)) {
setEmitFlags(typeNode, EmitFlags.SingleLine);
}
context.noInferenceFallback = oldNoInferenceFallback;
return notImplemented;
return syntacticResult(addUndefinedIfNeeded(typeNode, requiresAddingUndefined, objectLiteral, context));
}

function typeFromObjectLiteralPropertyAssignment(prop: PropertyAssignment, name: PropertyName, context: SyntacticTypeNodeBuilderContext, isConstContext: boolean) {
const modifiers = isConstContext ?
[factory.createModifier(SyntaxKind.ReadonlyKeyword)] :
[];
const expressionResult = typeFromExpression(prop.initializer, context, isConstContext);
const typeNode = expressionResult.type !== undefined ? expressionResult.type : inferTypeOfDeclaration(prop, /*symbol*/ undefined, context, expressionResult.reportFallback);
const typeNode = expressionResult.type !== undefined ? expressionResult.type : inferTypeOfDeclaration(prop, /*symbol*/ undefined, context, expressionResult.reportFallback, !isConstContext);

return factory.createPropertySignature(
modifiers,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10556,7 +10556,7 @@ export interface SyntacticTypeNodeBuilderResolver {
serializeExistingTypeNode(context: SyntacticTypeNodeBuilderContext, node: TypeNode, addUndefined?: boolean): TypeNode | undefined;
serializeReturnTypeForSignature(context: SyntacticTypeNodeBuilderContext, signatureDeclaration: SignatureDeclaration | JSDocSignature): TypeNode | undefined;
serializeTypeOfExpression(context: SyntacticTypeNodeBuilderContext, expr: Expression): TypeNode;
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined): TypeNode | undefined;
serializeTypeOfDeclaration(context: SyntacticTypeNodeBuilderContext, node: HasInferredType | GetAccessorDeclaration | SetAccessorDeclaration, symbol: Symbol | undefined, widen?: boolean): TypeNode | undefined;
serializeNameOfParameter(context: SyntacticTypeNodeBuilderContext, parameter: ParameterDeclaration): BindingName | string;
serializeTypeName(context: SyntacticTypeNodeBuilderContext, node: EntityName, isTypeOf?: boolean, typeArguments?: readonly TypeNode[]): TypeNode | undefined;
serializeEntityName(context: SyntacticTypeNodeBuilderContext, node: EntityNameExpression): Expression | undefined;
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of10.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== ES5For-of10.ts ===
function foo() {
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^

return { x: 0 };
>{ x: 0 } : { x: number; }
Expand All @@ -19,7 +19,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>[] : undefined[]
Expand All @@ -31,7 +31,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>[] : undefined[]
Expand All @@ -45,7 +45,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/ES5For-of34.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== ES5For-of34.ts ===
function foo() {
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^

return { x: 0 };
>{ x: 0 } : { x: number; }
Expand All @@ -19,7 +19,7 @@ for (foo().x of ['a', 'b', 'c']) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>['a', 'b', 'c'] : string[]
Expand All @@ -39,7 +39,7 @@ for (foo().x of ['a', 'b', 'c']) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/ES5For-of8.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== ES5For-of8.ts ===
function foo() {
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^

return { x: 0 };
>{ x: 0 } : { x: number; }
Expand All @@ -19,7 +19,7 @@ for (foo().x of ['a', 'b', 'c']) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>['a', 'b', 'c'] : string[]
Expand All @@ -39,7 +39,7 @@ for (foo().x of ['a', 'b', 'c']) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/ES5For-of9.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
=== ES5For-of9.ts ===
function foo() {
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^

return { x: 0 };
>{ x: 0 } : { x: number; }
Expand All @@ -19,7 +19,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>[] : undefined[]
Expand All @@ -31,7 +31,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
>[] : undefined[]
Expand All @@ -45,7 +45,7 @@ for (foo().x of []) {
>foo() : { x: number; }
> : ^^^^^^^^^^^^^^
>foo : () => { x: number; }
> : ^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^
>x : number
> : ^^^^^^
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ES5For-ofTypeCheck10.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MyStringIterator {

next() {
>next : () => { done: boolean; value: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^

return {
>{ done: true, value: "" } : { done: boolean; value: string; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module A {

export function Point() {
>Point : () => { x: number; y: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^

return { x: 0, y: 0 };
>{ x: 0, y: 0 } : { x: number; y: number; }
Expand Down Expand Up @@ -61,11 +61,11 @@ var fn = A.Point;
>fn : () => { x: number; y: number; }
> : ^^^^^^
>A.Point : () => { x: number; y: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>A : typeof A
> : ^^^^^^^^
>Point : () => { x: number; y: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^

var cl: { x: number; y: number; }
>cl : { x: number; y: number; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module A {

fromCarthesian(p: A.Point) {
>fromCarthesian : (p: A.Point) => { x: number; y: number; }
> : ^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>p : A.Point
> : ^^^^^^^
>A : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module A {

export function mirror<T extends Point>(p: T) {
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>p : T
> : ^

Expand Down Expand Up @@ -123,15 +123,15 @@ var o = A.Utils.mirror(o);
>A.Utils.mirror(o) : { x: number; y: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>A.Utils.mirror : <T extends A.Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>A.Utils : typeof A.Utils
> : ^^^^^^^^^^^^^^
>A : typeof A
> : ^^^^^^^^
>Utils : typeof A.Utils
> : ^^^^^^^^^^^^^^
>mirror : <T extends A.Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>o : { x: number; y: number; }
> : ^^^^^ ^^^^^ ^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export module A {

export function mirror<T extends Point>(p: T) {
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>p : T
> : ^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Root {

export function mirror<T extends Point>(p: T) {
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>p : T
> : ^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module A {

export function mirror<T extends Point>(p: T) {
>mirror : <T extends Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>p : T
> : ^

Expand Down Expand Up @@ -117,15 +117,15 @@ var o = A.Utils.mirror(o);
>A.Utils.mirror(o) : { x: number; y: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>A.Utils.mirror : <T extends A.Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>A.Utils : typeof A.Utils
> : ^^^^^^^^^^^^^^
>A : typeof A
> : ^^^^^^^^
>Utils : typeof A.Utils
> : ^^^^^^^^^^^^^^
>mirror : <T extends A.Point>(p: T) => { x: number; y: number; }
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^^^
>o : { x: number; y: number; }
> : ^^^^^ ^^^^^ ^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
=== /a.js ===
const a = () => {
>a : () => { arguments: any[]; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^
>() => { return { arguments: [], };} : () => { arguments: any[]; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^

return {
>{ arguments: [], } : { arguments: undefined[]; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class A {

public static createSelectableViewModel(initialState?: any, selectedValue?: any) {
>createSelectableViewModel : (initialState?: any, selectedValue?: any) => { selectedValue: number; }
> : ^ ^^^ ^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^ ^^^ ^^ ^^^ ^^^^^^^ ^^^^^^^^^^^
>initialState : any
>selectedValue : any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ declare var a: any;

const test = () => ({
>test : () => { prop: boolean; run: () => "special" | "default"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>() => ({ // "Identifier expected." error on "!" and two "Duplicate identifier '(Missing)'." errors on space. prop: !value, // remove ! to see that errors will be gone run: () => { //replace arrow function with regular function to see that errors will be gone // comment next line or remove "()" to see that errors will be gone if(!a.b()) { return 'special'; } return 'default'; }}) : () => { prop: boolean; run: () => "special" | "default"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> : ^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>({ // "Identifier expected." error on "!" and two "Duplicate identifier '(Missing)'." errors on space. prop: !value, // remove ! to see that errors will be gone run: () => { //replace arrow function with regular function to see that errors will be gone // comment next line or remove "()" to see that errors will be gone if(!a.b()) { return 'special'; } return 'default'; }}) : { prop: boolean; run: () => "special" | "default"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ // "Identifier expected." error on "!" and two "Duplicate identifier '(Missing)'." errors on space. prop: !value, // remove ! to see that errors will be gone run: () => { //replace arrow function with regular function to see that errors will be gone // comment next line or remove "()" to see that errors will be gone if(!a.b()) { return 'special'; } return 'default'; }} : { prop: boolean; run: () => "special" | "default"; }
Expand Down
Loading