From 57a3d2b5e98441ba7dd7d4d8f8f716298cb01e3e Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 15 Sep 2015 11:11:57 -0700 Subject: [PATCH] Accepted baselines. --- .../overloadsWithTypePredicates01.errors.txt | 58 +++++++++ .../overloadsWithTypePredicates01.js | 115 ++++++++++++++++++ .../overloadsWithTypePredicates02.errors.txt | 57 +++++++++ .../overloadsWithTypePredicates02.js | 110 +++++++++++++++++ .../overloadsWithTypePredicates03.errors.txt | 56 +++++++++ .../overloadsWithTypePredicates03.js | 81 ++++++++++++ 6 files changed, 477 insertions(+) create mode 100644 tests/baselines/reference/overloadsWithTypePredicates01.errors.txt create mode 100644 tests/baselines/reference/overloadsWithTypePredicates01.js create mode 100644 tests/baselines/reference/overloadsWithTypePredicates02.errors.txt create mode 100644 tests/baselines/reference/overloadsWithTypePredicates02.js create mode 100644 tests/baselines/reference/overloadsWithTypePredicates03.errors.txt create mode 100644 tests/baselines/reference/overloadsWithTypePredicates03.js diff --git a/tests/baselines/reference/overloadsWithTypePredicates01.errors.txt b/tests/baselines/reference/overloadsWithTypePredicates01.errors.txt new file mode 100644 index 00000000000..8eecea8a425 --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates01.errors.txt @@ -0,0 +1,58 @@ +tests/cases/conformance/functions/overloadsWithTypePredicates01.ts(35,10): error TS2394: Overload signature is not compatible with function implementation. + + +==== tests/cases/conformance/functions/overloadsWithTypePredicates01.ts (1 errors) ==== + + /** + * A makeshift string enum. + */ + namespace EventType { + export type Click = string & { _fooTag: any }; + export const Click = "Click"; + + export type KeyDown = string & { _barTag: any }; + export const KeyDown = "KeyDown"; + } + + /** + * The all-encompassing type for our makeshift enum. + */ + type EventType = EventType.Click + | EventType.KeyDown; + + + interface BaseEvent { + type: EventType; + } + + interface ClickEvent extends BaseEvent { + type: EventType.Click; + x: number; + y: number; + } + + interface KeyDownEvent extends BaseEvent { + type: EventType.KeyDown; + keyCode: number; + } + + function isActionType(action: BaseEvent, type: EventType.Click): action is ClickEvent; + ~~~~~~~~~~~~ +!!! error TS2394: Overload signature is not compatible with function implementation. + function isActionType(action: BaseEvent, type: EventType.KeyDown): action is KeyDownEvent; + function isActionType(action: BaseEvent, type: EventType): action is BaseEvent; + function isActionType(action: BaseEvent, type: EventType) { + return action.type === type; + } + + let handleAction = (action: BaseEvent) => { + if (isActionType(action, EventType.Click)) { + let foo = action.x; + let bar = action.y; + } + + if (isActionType(action, EventType.KeyDown)) { + let bar = action.keyCode; + } + } + \ No newline at end of file diff --git a/tests/baselines/reference/overloadsWithTypePredicates01.js b/tests/baselines/reference/overloadsWithTypePredicates01.js new file mode 100644 index 00000000000..ef54d31e89e --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates01.js @@ -0,0 +1,115 @@ +//// [overloadsWithTypePredicates01.ts] + +/** + * A makeshift string enum. + */ +namespace EventType { + export type Click = string & { _fooTag: any }; + export const Click = "Click"; + + export type KeyDown = string & { _barTag: any }; + export const KeyDown = "KeyDown"; +} + +/** + * The all-encompassing type for our makeshift enum. + */ +type EventType = EventType.Click + | EventType.KeyDown; + + +interface BaseEvent { + type: EventType; +} + +interface ClickEvent extends BaseEvent { + type: EventType.Click; + x: number; + y: number; +} + +interface KeyDownEvent extends BaseEvent { + type: EventType.KeyDown; + keyCode: number; +} + +function isActionType(action: BaseEvent, type: EventType.Click): action is ClickEvent; +function isActionType(action: BaseEvent, type: EventType.KeyDown): action is KeyDownEvent; +function isActionType(action: BaseEvent, type: EventType): action is BaseEvent; +function isActionType(action: BaseEvent, type: EventType) { + return action.type === type; +} + +let handleAction = (action: BaseEvent) => { + if (isActionType(action, EventType.Click)) { + let foo = action.x; + let bar = action.y; + } + + if (isActionType(action, EventType.KeyDown)) { + let bar = action.keyCode; + } +} + + +//// [overloadsWithTypePredicates01.js] +/** + * A makeshift string enum. + */ +var EventType; +(function (EventType) { + EventType.Click = "Click"; + EventType.KeyDown = "KeyDown"; +})(EventType || (EventType = {})); +function isActionType(action, type) { + return action.type === type; +} +var handleAction = function (action) { + if (isActionType(action, EventType.Click)) { + var foo = action.x; + var bar = action.y; + } + if (isActionType(action, EventType.KeyDown)) { + var bar = action.keyCode; + } +}; + + +//// [overloadsWithTypePredicates01.d.ts] +/** + * A makeshift string enum. + */ +declare namespace EventType { + type Click = string & { + _fooTag: any; + }; + const Click: string & { + _fooTag: any; + }; + type KeyDown = string & { + _barTag: any; + }; + const KeyDown: string & { + _barTag: any; + }; +} +/** + * The all-encompassing type for our makeshift enum. + */ +declare type EventType = EventType.Click | EventType.KeyDown; +interface BaseEvent { + type: EventType; +} +interface ClickEvent extends BaseEvent { + type: EventType.Click; + x: number; + y: number; +} +interface KeyDownEvent extends BaseEvent { + type: EventType.KeyDown; + keyCode: number; +} +declare function isActionType(action: BaseEvent, type: EventType.Click): action is ClickEvent; +declare function isActionType(action: BaseEvent, type: EventType.KeyDown): action is KeyDownEvent; +declare function isActionType(action: BaseEvent, type: EventType): action is BaseEvent; +declare let handleAction: (action: BaseEvent) => void; diff --git a/tests/baselines/reference/overloadsWithTypePredicates02.errors.txt b/tests/baselines/reference/overloadsWithTypePredicates02.errors.txt new file mode 100644 index 00000000000..6ff4bf9e9a4 --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates02.errors.txt @@ -0,0 +1,57 @@ +tests/cases/conformance/functions/overloadsWithTypePredicates02.ts(22,10): error TS2394: Overload signature is not compatible with function implementation. + + +==== tests/cases/conformance/functions/overloadsWithTypePredicates02.ts (1 errors) ==== + + interface Nil { + } + + interface Cons { + value: T; + next: List; + } + + type List = Cons | Nil; + const nil: Nil = {}; + function cons(value: T, next: List) { + return { value, next }; + } + + function hasElements(list: Cons): list is Cons; + function hasElements(list: List): list is Cons; + function hasElements(list: List): list is Cons { + return !!(list as Cons).next; + } + + function isEmpty(list: Nil): list is Nil; + ~~~~~~~ +!!! error TS2394: Overload signature is not compatible with function implementation. + function isEmpty(list: List): list is Nil; + function isEmpty(list: List): boolean { + return !isEmpty(list); + } + + let listA = cons(1, cons(2, cons(3, nil))); + let listB = nil; + let listC: List = listA || listB; + + if (isEmpty(listA)) { + let a = listA; + } + else { + let a = listA; + } + + if (hasElements(listC)) { + let { value } = listC; + } + else { + let myNil: Nil = listC; + } + + if (hasElements(listB)) { + let somehowCons: Cons = listB; + } + else { + let myNil: Nil = listB; + } \ No newline at end of file diff --git a/tests/baselines/reference/overloadsWithTypePredicates02.js b/tests/baselines/reference/overloadsWithTypePredicates02.js new file mode 100644 index 00000000000..68853ea08d1 --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates02.js @@ -0,0 +1,110 @@ +//// [overloadsWithTypePredicates02.ts] + +interface Nil { +} + +interface Cons { + value: T; + next: List; +} + +type List = Cons | Nil; +const nil: Nil = {}; +function cons(value: T, next: List) { + return { value, next }; +} + +function hasElements(list: Cons): list is Cons; +function hasElements(list: List): list is Cons; +function hasElements(list: List): list is Cons { + return !!(list as Cons).next; +} + +function isEmpty(list: Nil): list is Nil; +function isEmpty(list: List): list is Nil; +function isEmpty(list: List): boolean { + return !isEmpty(list); +} + +let listA = cons(1, cons(2, cons(3, nil))); +let listB = nil; +let listC: List = listA || listB; + +if (isEmpty(listA)) { + let a = listA; +} +else { + let a = listA; +} + +if (hasElements(listC)) { + let { value } = listC; +} +else { + let myNil: Nil = listC; +} + +if (hasElements(listB)) { + let somehowCons: Cons = listB; +} +else { + let myNil: Nil = listB; +} + +//// [overloadsWithTypePredicates02.js] +var nil = {}; +function cons(value, next) { + return { value: value, next: next }; +} +function hasElements(list) { + return !!list.next; +} +function isEmpty(list) { + return !isEmpty(list); +} +var listA = cons(1, cons(2, cons(3, nil))); +var listB = nil; +var listC = listA || listB; +if (isEmpty(listA)) { + var a = listA; +} +else { + var a = listA; +} +if (hasElements(listC)) { + var value = listC.value; +} +else { + var myNil = listC; +} +if (hasElements(listB)) { + var somehowCons = listB; +} +else { + var myNil = listB; +} + + +//// [overloadsWithTypePredicates02.d.ts] +interface Nil { +} +interface Cons { + value: T; + next: List; +} +declare type List = Cons | Nil; +declare const nil: Nil; +declare function cons(value: T, next: List): { + value: T; + next: Cons | Nil; +}; +declare function hasElements(list: Cons): list is Cons; +declare function hasElements(list: List): list is Cons; +declare function isEmpty(list: Nil): list is Nil; +declare function isEmpty(list: List): list is Nil; +declare let listA: { + value: number; + next: Cons | Nil; +}; +declare let listB: Nil; +declare let listC: List; diff --git a/tests/baselines/reference/overloadsWithTypePredicates03.errors.txt b/tests/baselines/reference/overloadsWithTypePredicates03.errors.txt new file mode 100644 index 00000000000..7ff18070aaf --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates03.errors.txt @@ -0,0 +1,56 @@ +tests/cases/conformance/functions/overloadsWithTypePredicates03.ts(2,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/functions/overloadsWithTypePredicates03.ts(3,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/functions/overloadsWithTypePredicates03.ts(4,10): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. +tests/cases/conformance/functions/overloadsWithTypePredicates03.ts(5,10): error TS2394: Overload signature is not compatible with function implementation. + + +==== tests/cases/conformance/functions/overloadsWithTypePredicates03.ts (4 errors) ==== + + function is(x: any, type: "number"): x is number; + ~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function is(x: any, type: "string"): x is string; + ~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function is(x: any, type: "boolean"): x is boolean; + ~~ +!!! error TS2382: Specialized overload signature is not assignable to any non-specialized signature. + function is(x: any, type: string): x is (number | boolean | string); + ~~ +!!! error TS2394: Overload signature is not compatible with function implementation. + function is(x: any, type: string): boolean { + if (["string", "number", "boolean"].indexOf(type) >= 0) { + return typeof x === type; + } + + return false; + } + + declare function myRand(): boolean; + + let strNumOrBool: string | number | boolean; + + if (myRand()) { + strNumOrBool = "abc"; + } + else if (myRand()) { + strNumOrBool = 100; + } + else { + strNumOrBool = true; + } + + if (is(strNumOrBool, "number")) { + let num = strNumOrBool; + num *= 100; + } + + if (is(strNumOrBool, "string")) { + let str = strNumOrBool; + str = str.slice(); + } + + if (is(strNumOrBool, "boolean")) { + let bool = strNumOrBool; + bool = bool || bool && bool; + } \ No newline at end of file diff --git a/tests/baselines/reference/overloadsWithTypePredicates03.js b/tests/baselines/reference/overloadsWithTypePredicates03.js new file mode 100644 index 00000000000..443859534fa --- /dev/null +++ b/tests/baselines/reference/overloadsWithTypePredicates03.js @@ -0,0 +1,81 @@ +//// [overloadsWithTypePredicates03.ts] + +function is(x: any, type: "number"): x is number; +function is(x: any, type: "string"): x is string; +function is(x: any, type: "boolean"): x is boolean; +function is(x: any, type: string): x is (number | boolean | string); +function is(x: any, type: string): boolean { + if (["string", "number", "boolean"].indexOf(type) >= 0) { + return typeof x === type; + } + + return false; +} + +declare function myRand(): boolean; + +let strNumOrBool: string | number | boolean; + +if (myRand()) { + strNumOrBool = "abc"; +} +else if (myRand()) { + strNumOrBool = 100; +} +else { + strNumOrBool = true; +} + +if (is(strNumOrBool, "number")) { + let num = strNumOrBool; + num *= 100; +} + +if (is(strNumOrBool, "string")) { + let str = strNumOrBool; + str = str.slice(); +} + +if (is(strNumOrBool, "boolean")) { + let bool = strNumOrBool; + bool = bool || bool && bool; +} + +//// [overloadsWithTypePredicates03.js] +function is(x, type) { + if (["string", "number", "boolean"].indexOf(type) >= 0) { + return typeof x === type; + } + return false; +} +var strNumOrBool; +if (myRand()) { + strNumOrBool = "abc"; +} +else if (myRand()) { + strNumOrBool = 100; +} +else { + strNumOrBool = true; +} +if (is(strNumOrBool, "number")) { + var num = strNumOrBool; + num *= 100; +} +if (is(strNumOrBool, "string")) { + var str = strNumOrBool; + str = str.slice(); +} +if (is(strNumOrBool, "boolean")) { + var bool = strNumOrBool; + bool = bool || bool && bool; +} + + +//// [overloadsWithTypePredicates03.d.ts] +declare function is(x: any, type: "number"): x is number; +declare function is(x: any, type: "string"): x is string; +declare function is(x: any, type: "boolean"): x is boolean; +declare function is(x: any, type: string): x is (number | boolean | string); +declare function myRand(): boolean; +declare let strNumOrBool: string | number | boolean;