Skip to content

Allow intersected numbers in arithmetics & element access #4373

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

Closed
wants to merge 5 commits into from
Closed
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
16 changes: 13 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9612,20 +9612,30 @@ namespace ts {
return false;
}

// Return true if type has the given flags, or is a union or intersection type composed of types that all have those flags.
// Return true if type has the given flags,
// is a union type composed of types that all have those flags,
// or an intersection type that contains (at least) one type that has those flags.
function allConstituentTypesHaveKind(type: Type, kind: TypeFlags): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

I think that the name of this function is now misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree about that, do you have a better name? Maybe just typeHasKind?

if (type.flags & kind) {
return true;
}
if (type.flags & TypeFlags.UnionOrIntersection) {
let types = (<UnionOrIntersectionType>type).types;
if (type.flags & TypeFlags.Union) {
let types = (<UnionType>type).types;
for (let current of types) {
if (!(current.flags & kind)) {
return false;
}
}
return true;
}
if (type.flags & TypeFlags.Intersection) {
let types = (<IntersectionType>type).types;
for (let current of types) {
if (current.flags & kind) {
return true;
}
}
}
return false;
}

Expand Down
33 changes: 33 additions & 0 deletions tests/baselines/reference/intersectionArithmetic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [intersectionArithmetic.ts]
let numberObject: number & { foo: any };
let stringObject: string & { foo: any };
let numberString: number & string;

let a = numberObject + 1;
let b = stringObject + 1;
let c = numberObject + '';
let d = stringObject + '';
let e = numberObject + stringObject;

let f = numberString + 1;
let g = numberString + '';
let h = numberString + numberString;

let i = numberObject * 2;
let j = numberString * 2;


//// [intersectionArithmetic.js]
var numberObject;
var stringObject;
var numberString;
var a = numberObject + 1;
var b = stringObject + 1;
var c = numberObject + '';
var d = stringObject + '';
var e = numberObject + stringObject;
var f = numberString + 1;
var g = numberString + '';
var h = numberString + numberString;
var i = numberObject * 2;
var j = numberString * 2;
54 changes: 54 additions & 0 deletions tests/baselines/reference/intersectionArithmetic.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
=== tests/cases/conformance/types/intersection/intersectionArithmetic.ts ===
let numberObject: number & { foo: any };
>numberObject : Symbol(numberObject, Decl(intersectionArithmetic.ts, 0, 3))
>foo : Symbol(foo, Decl(intersectionArithmetic.ts, 0, 28))

let stringObject: string & { foo: any };
>stringObject : Symbol(stringObject, Decl(intersectionArithmetic.ts, 1, 3))
>foo : Symbol(foo, Decl(intersectionArithmetic.ts, 1, 28))

let numberString: number & string;
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))

let a = numberObject + 1;
>a : Symbol(a, Decl(intersectionArithmetic.ts, 4, 3))
>numberObject : Symbol(numberObject, Decl(intersectionArithmetic.ts, 0, 3))

let b = stringObject + 1;
>b : Symbol(b, Decl(intersectionArithmetic.ts, 5, 3))
>stringObject : Symbol(stringObject, Decl(intersectionArithmetic.ts, 1, 3))

let c = numberObject + '';
>c : Symbol(c, Decl(intersectionArithmetic.ts, 6, 3))
>numberObject : Symbol(numberObject, Decl(intersectionArithmetic.ts, 0, 3))

let d = stringObject + '';
>d : Symbol(d, Decl(intersectionArithmetic.ts, 7, 3))
>stringObject : Symbol(stringObject, Decl(intersectionArithmetic.ts, 1, 3))

let e = numberObject + stringObject;
>e : Symbol(e, Decl(intersectionArithmetic.ts, 8, 3))
>numberObject : Symbol(numberObject, Decl(intersectionArithmetic.ts, 0, 3))
>stringObject : Symbol(stringObject, Decl(intersectionArithmetic.ts, 1, 3))

let f = numberString + 1;
>f : Symbol(f, Decl(intersectionArithmetic.ts, 10, 3))
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))

let g = numberString + '';
>g : Symbol(g, Decl(intersectionArithmetic.ts, 11, 3))
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))

let h = numberString + numberString;
>h : Symbol(h, Decl(intersectionArithmetic.ts, 12, 3))
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))

let i = numberObject * 2;
>i : Symbol(i, Decl(intersectionArithmetic.ts, 14, 3))
>numberObject : Symbol(numberObject, Decl(intersectionArithmetic.ts, 0, 3))

let j = numberString * 2;
>j : Symbol(j, Decl(intersectionArithmetic.ts, 15, 3))
>numberString : Symbol(numberString, Decl(intersectionArithmetic.ts, 2, 3))

72 changes: 72 additions & 0 deletions tests/baselines/reference/intersectionArithmetic.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
=== tests/cases/conformance/types/intersection/intersectionArithmetic.ts ===
let numberObject: number & { foo: any };
>numberObject : number & { foo: any; }
>foo : any

let stringObject: string & { foo: any };
>stringObject : string & { foo: any; }
>foo : any

let numberString: number & string;
>numberString : number & string

let a = numberObject + 1;
>a : number
>numberObject + 1 : number
>numberObject : number & { foo: any; }
>1 : number

let b = stringObject + 1;
>b : string
>stringObject + 1 : string
>stringObject : string & { foo: any; }
>1 : number

let c = numberObject + '';
>c : string
>numberObject + '' : string
>numberObject : number & { foo: any; }
>'' : string

let d = stringObject + '';
>d : string
>stringObject + '' : string
>stringObject : string & { foo: any; }
>'' : string

let e = numberObject + stringObject;
>e : string
>numberObject + stringObject : string
>numberObject : number & { foo: any; }
>stringObject : string & { foo: any; }

let f = numberString + 1;
>f : number
>numberString + 1 : number
>numberString : number & string
>1 : number

let g = numberString + '';
>g : string
>numberString + '' : string
>numberString : number & string
>'' : string

let h = numberString + numberString;
>h : number
Copy link
Member

Choose a reason for hiding this comment

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

This is very "interesting" behavior. I'm not sure what the correct thing to do here would be, but even the spec says that there are no meaningful values for intersections of primitives.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know what the best behavior is here, but given that there is no way in JavaScript to create a type that is both a string and a number, it doesn't matter very much in my opinion. I think the question we should ask is: which of these rules should be used?

  1. Result of + is string if at least one operand is a string, otherwise number
  2. Result of + is number if both operands are number, otherwise string

In case 1, the result should be string, in case 2 number

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only values that are both a number and a string are null and undefined. Adding them results in:

undefined + undefined = NaN
undefined + null = NaN
null + undefined = NaN
null + null = 0

All results are numbers, so I think the current behavior is correct.

>numberString + numberString : number
>numberString : number & string
>numberString : number & string

let i = numberObject * 2;
>i : number
>numberObject * 2 : number
>numberObject : number & { foo: any; }
>2 : number

let j = numberString * 2;
>j : number
>numberString * 2 : number
>numberString : number & string
>2 : number

23 changes: 23 additions & 0 deletions tests/baselines/reference/intersectionElementAccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [intersectionElementAccess.ts]
let numberObject: number & { foo: any };
let stringObject: string & { foo: any };
let numberString: number & string;

let object: {
[ index: number ]: { stringIndexer: any, numberIndexer: any },
[ key: string]: { stringIndexer: any }
}

let a = object[numberObject];
let b = object[stringObject];
let c = object[numberString];


//// [intersectionElementAccess.js]
var numberObject;
var stringObject;
var numberString;
var object;
var a = object[numberObject];
var b = object[stringObject];
var c = object[numberString];
40 changes: 40 additions & 0 deletions tests/baselines/reference/intersectionElementAccess.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/conformance/types/intersection/intersectionElementAccess.ts ===
let numberObject: number & { foo: any };
>numberObject : Symbol(numberObject, Decl(intersectionElementAccess.ts, 0, 3))
>foo : Symbol(foo, Decl(intersectionElementAccess.ts, 0, 28))

let stringObject: string & { foo: any };
>stringObject : Symbol(stringObject, Decl(intersectionElementAccess.ts, 1, 3))
>foo : Symbol(foo, Decl(intersectionElementAccess.ts, 1, 28))

let numberString: number & string;
>numberString : Symbol(numberString, Decl(intersectionElementAccess.ts, 2, 3))

let object: {
>object : Symbol(object, Decl(intersectionElementAccess.ts, 4, 3))

[ index: number ]: { stringIndexer: any, numberIndexer: any },
>index : Symbol(index, Decl(intersectionElementAccess.ts, 5, 2))
>stringIndexer : Symbol(stringIndexer, Decl(intersectionElementAccess.ts, 5, 21))
>numberIndexer : Symbol(numberIndexer, Decl(intersectionElementAccess.ts, 5, 41))

[ key: string]: { stringIndexer: any }
>key : Symbol(key, Decl(intersectionElementAccess.ts, 6, 2))
>stringIndexer : Symbol(stringIndexer, Decl(intersectionElementAccess.ts, 6, 18))
}

let a = object[numberObject];
>a : Symbol(a, Decl(intersectionElementAccess.ts, 9, 3))
>object : Symbol(object, Decl(intersectionElementAccess.ts, 4, 3))
>numberObject : Symbol(numberObject, Decl(intersectionElementAccess.ts, 0, 3))

let b = object[stringObject];
>b : Symbol(b, Decl(intersectionElementAccess.ts, 10, 3))
>object : Symbol(object, Decl(intersectionElementAccess.ts, 4, 3))
>stringObject : Symbol(stringObject, Decl(intersectionElementAccess.ts, 1, 3))

let c = object[numberString];
>c : Symbol(c, Decl(intersectionElementAccess.ts, 11, 3))
>object : Symbol(object, Decl(intersectionElementAccess.ts, 4, 3))
>numberString : Symbol(numberString, Decl(intersectionElementAccess.ts, 2, 3))

43 changes: 43 additions & 0 deletions tests/baselines/reference/intersectionElementAccess.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
=== tests/cases/conformance/types/intersection/intersectionElementAccess.ts ===
let numberObject: number & { foo: any };
>numberObject : number & { foo: any; }
>foo : any

let stringObject: string & { foo: any };
>stringObject : string & { foo: any; }
>foo : any

let numberString: number & string;
>numberString : number & string

let object: {
>object : { [key: string]: { stringIndexer: any; }; [index: number]: { stringIndexer: any; numberIndexer: any; }; }

[ index: number ]: { stringIndexer: any, numberIndexer: any },
>index : number
>stringIndexer : any
>numberIndexer : any

[ key: string]: { stringIndexer: any }
>key : string
>stringIndexer : any
}

let a = object[numberObject];
>a : { stringIndexer: any; numberIndexer: any; }
>object[numberObject] : { stringIndexer: any; numberIndexer: any; }
>object : { [key: string]: { stringIndexer: any; }; [index: number]: { stringIndexer: any; numberIndexer: any; }; }
>numberObject : number & { foo: any; }

let b = object[stringObject];
>b : { stringIndexer: any; }
>object[stringObject] : { stringIndexer: any; }
>object : { [key: string]: { stringIndexer: any; }; [index: number]: { stringIndexer: any; numberIndexer: any; }; }
>stringObject : string & { foo: any; }

let c = object[numberString];
>c : { stringIndexer: any; numberIndexer: any; }
>object[numberString] : { stringIndexer: any; numberIndexer: any; }
>object : { [key: string]: { stringIndexer: any; }; [index: number]: { stringIndexer: any; numberIndexer: any; }; }
>numberString : number & string

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let numberObject: number & { foo: any };
let stringObject: string & { foo: any };
let numberString: number & string;

let a = numberObject + 1;
let b = stringObject + 1;
let c = numberObject + '';
let d = stringObject + '';
let e = numberObject + stringObject;

let f = numberString + 1;
let g = numberString + '';
let h = numberString + numberString;

let i = numberObject * 2;
let j = numberString * 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let numberObject: number & { foo: any };
let stringObject: string & { foo: any };
let numberString: number & string;

let object: {
[ index: number ]: { stringIndexer: any, numberIndexer: any },
[ key: string]: { stringIndexer: any }
}

let a = object[numberObject];
let b = object[stringObject];
let c = object[numberString];