Skip to content

Slightly less conservative check in isConstraintPosition #46526

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

Merged
merged 2 commits into from
Oct 26, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24755,7 +24755,7 @@ namespace ts {
return parent.kind === SyntaxKind.PropertyAccessExpression ||
parent.kind === SyntaxKind.CallExpression && (parent as CallExpression).expression === node ||
parent.kind === SyntaxKind.ElementAccessExpression && (parent as ElementAccessExpression).expression === node &&
!(isGenericTypeWithoutNullableConstraint(type) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
!(someType(type, isGenericTypeWithoutNullableConstraint) && isGenericIndexType(getTypeOfExpression((parent as ElementAccessExpression).argumentExpression)));
}

function isGenericTypeWithUnionConstraint(type: Type) {
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/controlFlowGenericTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,24 @@ tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts(168,9): error TS2
this.validateRow(row);
}
}

// Repro from #46495

interface Button {
type: "button";
text: string;
}

interface Checkbox {
type: "checkbox";
isChecked: boolean;
}

type Control = Button | Checkbox;

function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
if (control !== undefined) {
control[key] = value;
}
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/controlFlowGenericTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ class SqlTable<T> {
this.validateRow(row);
}
}

// Repro from #46495

interface Button {
type: "button";
text: string;
}

interface Checkbox {
type: "checkbox";
isChecked: boolean;
}

type Control = Button | Checkbox;

function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
if (control !== undefined) {
control[key] = value;
}
}


//// [controlFlowGenericTypes.js]
Expand Down Expand Up @@ -343,3 +363,8 @@ var SqlTable = /** @class */ (function () {
};
return SqlTable;
}());
function update(control, key, value) {
if (control !== undefined) {
control[key] = value;
}
}
52 changes: 52 additions & 0 deletions tests/baselines/reference/controlFlowGenericTypes.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,55 @@ class SqlTable<T> {
}
}

// Repro from #46495

interface Button {
>Button : Symbol(Button, Decl(controlFlowGenericTypes.ts, 190, 1))

type: "button";
>type : Symbol(Button.type, Decl(controlFlowGenericTypes.ts, 194, 18))

text: string;
>text : Symbol(Button.text, Decl(controlFlowGenericTypes.ts, 195, 19))
}

interface Checkbox {
>Checkbox : Symbol(Checkbox, Decl(controlFlowGenericTypes.ts, 197, 1))

type: "checkbox";
>type : Symbol(Checkbox.type, Decl(controlFlowGenericTypes.ts, 199, 20))

isChecked: boolean;
>isChecked : Symbol(Checkbox.isChecked, Decl(controlFlowGenericTypes.ts, 200, 21))
}

type Control = Button | Checkbox;
>Control : Symbol(Control, Decl(controlFlowGenericTypes.ts, 202, 1))
>Button : Symbol(Button, Decl(controlFlowGenericTypes.ts, 190, 1))
>Checkbox : Symbol(Checkbox, Decl(controlFlowGenericTypes.ts, 197, 1))

function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
>update : Symbol(update, Decl(controlFlowGenericTypes.ts, 204, 33))
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 206, 16))
>Control : Symbol(Control, Decl(controlFlowGenericTypes.ts, 202, 1))
>K : Symbol(K, Decl(controlFlowGenericTypes.ts, 206, 34))
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 206, 16))
>control : Symbol(control, Decl(controlFlowGenericTypes.ts, 206, 54))
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 206, 16))
>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 206, 78))
>K : Symbol(K, Decl(controlFlowGenericTypes.ts, 206, 34))
>value : Symbol(value, Decl(controlFlowGenericTypes.ts, 206, 86))
>T : Symbol(T, Decl(controlFlowGenericTypes.ts, 206, 16))
>K : Symbol(K, Decl(controlFlowGenericTypes.ts, 206, 34))

if (control !== undefined) {
>control : Symbol(control, Decl(controlFlowGenericTypes.ts, 206, 54))
>undefined : Symbol(undefined)

control[key] = value;
>control : Symbol(control, Decl(controlFlowGenericTypes.ts, 206, 54))
>key : Symbol(key, Decl(controlFlowGenericTypes.ts, 206, 78))
>value : Symbol(value, Decl(controlFlowGenericTypes.ts, 206, 86))
}
}

41 changes: 41 additions & 0 deletions tests/baselines/reference/controlFlowGenericTypes.types
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,44 @@ class SqlTable<T> {
}
}

// Repro from #46495

interface Button {
type: "button";
>type : "button"

text: string;
>text : string
}

interface Checkbox {
type: "checkbox";
>type : "checkbox"

isChecked: boolean;
>isChecked : boolean
}

type Control = Button | Checkbox;
>Control : Control

function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
>update : <T extends Control, K extends keyof T>(control: T | undefined, key: K, value: T[K]) => void
>control : T | undefined
>key : K
>value : T[K]

if (control !== undefined) {
>control !== undefined : boolean
>control : T | undefined
>undefined : undefined

control[key] = value;
>control[key] = value : T[K]
>control[key] : T[K]
>control : T
>key : K
>value : T[K]
}
}

20 changes: 20 additions & 0 deletions tests/cases/conformance/controlFlow/controlFlowGenericTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,23 @@ class SqlTable<T> {
this.validateRow(row);
}
}

// Repro from #46495

interface Button {
type: "button";
text: string;
}

interface Checkbox {
type: "checkbox";
isChecked: boolean;
}

type Control = Button | Checkbox;

function update<T extends Control, K extends keyof T>(control : T | undefined, key: K, value: T[K]): void {
if (control !== undefined) {
control[key] = value;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a subsequent else if discriminate between Button and Checkbox, or are we now stuck with T?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a subsequent else if the type of control would be undefined. But if you mean a subsequent if inside the block, then yes:

if (control !== undefined) {          // Type of control is T | undefined
    control[key] = value;             // Type of control is T
    if (control.type === "button") {  // Type of control is Control
        control.text;                 // Type of control is Button
    }
}

}