Skip to content

Mark fresh spread types with ContainsObjectLiteral #19250

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 17, 2017
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 @@ -7971,7 +7971,7 @@ namespace ts {

const spread = createAnonymousType(undefined, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
spread.flags |= propagatedFlags;
spread.flags |= TypeFlags.FreshLiteral;
spread.flags |= TypeFlags.FreshLiteral | TypeFlags.ContainsObjectLiteral;
(spread as ObjectType).objectFlags |= ObjectFlags.ObjectLiteral;
spread.symbol = symbol;
return spread;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(40,1): error TS2322: Type
// these two are not reported because there are two discriminant properties
over = { a: 1, b: 1, first: "ok", second: "error" }
over = { a: 1, b: 1, first: "ok", third: "error" }

// Freshness disappears after spreading a union
declare let t0: { a: any, b: any } | { d: any, e: any }
declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
let t2 = { ...t1 }
t0 = t2

17 changes: 17 additions & 0 deletions tests/baselines/reference/excessPropertyCheckWithUnions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@ let over: Overlapping
// these two are not reported because there are two discriminant properties
over = { a: 1, b: 1, first: "ok", second: "error" }
over = { a: 1, b: 1, first: "ok", third: "error" }

// Freshness disappears after spreading a union
declare let t0: { a: any, b: any } | { d: any, e: any }
declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
let t2 = { ...t1 }
t0 = t2


//// [excessPropertyCheckWithUnions.js]
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
var wrong = { tag: "T", a1: "extra" };
wrong = { tag: "A", d20: 12 };
wrong = { tag: "D" };
Expand All @@ -72,3 +87,5 @@ var over;
// these two are not reported because there are two discriminant properties
over = { a: 1, b: 1, first: "ok", second: "error" };
over = { a: 1, b: 1, first: "ok", third: "error" };
var t2 = __assign({}, t1);
t0 = t2;
25 changes: 25 additions & 0 deletions tests/baselines/reference/excessPropertyCheckWithUnions.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,28 @@ over = { a: 1, b: 1, first: "ok", third: "error" }
>first : Symbol(first, Decl(excessPropertyCheckWithUnions.ts, 49, 20))
>third : Symbol(third, Decl(excessPropertyCheckWithUnions.ts, 49, 33))

// Freshness disappears after spreading a union
declare let t0: { a: any, b: any } | { d: any, e: any }
>t0 : Symbol(t0, Decl(excessPropertyCheckWithUnions.ts, 52, 11))
>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 52, 17))
>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 52, 25))
>d : Symbol(d, Decl(excessPropertyCheckWithUnions.ts, 52, 38))
>e : Symbol(e, Decl(excessPropertyCheckWithUnions.ts, 52, 46))

declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
>t1 : Symbol(t1, Decl(excessPropertyCheckWithUnions.ts, 53, 11))
>a : Symbol(a, Decl(excessPropertyCheckWithUnions.ts, 53, 17))
>b : Symbol(b, Decl(excessPropertyCheckWithUnions.ts, 53, 25))
>c : Symbol(c, Decl(excessPropertyCheckWithUnions.ts, 53, 33))
>c : Symbol(c, Decl(excessPropertyCheckWithUnions.ts, 53, 46))
>d : Symbol(d, Decl(excessPropertyCheckWithUnions.ts, 53, 54))
>e : Symbol(e, Decl(excessPropertyCheckWithUnions.ts, 53, 62))

let t2 = { ...t1 }
>t2 : Symbol(t2, Decl(excessPropertyCheckWithUnions.ts, 54, 3))
>t1 : Symbol(t1, Decl(excessPropertyCheckWithUnions.ts, 53, 11))

t0 = t2
>t0 : Symbol(t0, Decl(excessPropertyCheckWithUnions.ts, 52, 11))
>t2 : Symbol(t2, Decl(excessPropertyCheckWithUnions.ts, 54, 3))

27 changes: 27 additions & 0 deletions tests/baselines/reference/excessPropertyCheckWithUnions.types
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,30 @@ over = { a: 1, b: 1, first: "ok", third: "error" }
>third : string
>"error" : "error"

// Freshness disappears after spreading a union
declare let t0: { a: any, b: any } | { d: any, e: any }
>t0 : { a: any; b: any; } | { d: any; e: any; }
>a : any
>b : any
>d : any
>e : any

declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
>t1 : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }
>a : any
>b : any
>c : any
>c : any
>d : any
>e : any

let t2 = { ...t1 }
>t2 : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }
>{ ...t1 } : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }
>t1 : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }

t0 = t2
>t0 = t2 : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }
>t0 : { a: any; b: any; } | { d: any; e: any; }
>t2 : { a: any; b: any; c: any; } | { c: any; d: any; e: any; }

2 changes: 1 addition & 1 deletion tests/baselines/reference/spreadInvalidArgumentType.types
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function f<T extends { b: string }>(p1: T, p2: T[]) {
>p1 : T

var o2 = { ...p2 }; // OK
>o2 : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray<T>[]): T[]; concat(...items: (T | ReadonlyArray<T>)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; }
>o2 : { [x: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray<T>[]): T[]; concat(...items: (T | ReadonlyArray<T>)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; }
>{ ...p2 } : { [n: number]: T; length: number; toString(): string; toLocaleString(): string; push(...items: T[]): number; pop(): T; concat(...items: ReadonlyArray<T>[]): T[]; concat(...items: (T | ReadonlyArray<T>)[]): T[]; join(separator?: string): string; reverse(): T[]; shift(): T; slice(start?: number, end?: number): T[]; sort(compareFn?: (a: T, b: T) => number): T[]; splice(start: number, deleteCount?: number): T[]; splice(start: number, deleteCount: number, ...items: T[]): T[]; unshift(...items: T[]): number; indexOf(searchElement: T, fromIndex?: number): number; lastIndexOf(searchElement: T, fromIndex?: number): number; every(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; some(callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: any): boolean; forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; filter<S extends T>(callbackfn: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; filter(callbackfn: (value: T, index: number, array: T[]) => any, thisArg?: any): T[]; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; reduceRight<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; }
>p2 : T[]

Expand Down
7 changes: 7 additions & 0 deletions tests/cases/compiler/excessPropertyCheckWithUnions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @strict: true
type ADT = {
tag: "A",
a1: string
Expand Down Expand Up @@ -48,3 +49,9 @@ let over: Overlapping
// these two are not reported because there are two discriminant properties
over = { a: 1, b: 1, first: "ok", second: "error" }
over = { a: 1, b: 1, first: "ok", third: "error" }

// Freshness disappears after spreading a union
declare let t0: { a: any, b: any } | { d: any, e: any }
declare let t1: { a: any, b: any, c: any } | { c: any, d: any, e: any }
let t2 = { ...t1 }
t0 = t2