• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Copyright (c) 2023 Huawei Device Co., Ltd.
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*
16* This file has been modified by Huawei to verify type inference by adding verification statements.
17*/
18
19// === tests/cases/compiler/typeInferenceLiteralUnion.ts ===
20declare function AssertType(value:any, type:string):void;
21// Repro from #10901
22/**
23 * Administrivia: JavaScript primitive types and Date
24 */
25export type Primitive = number | string | boolean | Date;
26
27/**
28 * Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
29 */
30interface Numeric {
31    valueOf(): number;
32}
33
34// Not very useful, but meets Numeric
35class NumCoercible {
36    public a: number;
37
38    constructor(a: number) {
39        this.a = a;
40AssertType(this.a = a, "number");
41AssertType(this.a, "number");
42AssertType(this, "this");
43AssertType(a, "number");
44    }
45    public valueOf() {
46AssertType(this.a, "number");
47AssertType(this, "this");
48        return this.a;
49    }
50}
51
52/**
53 * Return the min and max simultaneously.
54 */
55export function extent<T extends Numeric>(array: Array<T | Primitive>): [T | Primitive, T | Primitive] | [undefined, undefined] {
56AssertType([undefined, undefined], "[undefined, undefined]");
57AssertType(undefined, "undefined");
58AssertType(undefined, "undefined");
59    return [undefined, undefined];
60}
61
62
63let extentMixed: [Primitive | NumCoercible, Primitive | NumCoercible] | [undefined, undefined];
64AssertType(extentMixed, "union");
65
66extentMixed = extent([new NumCoercible(10), 13, '12', true]);
67AssertType(extentMixed = extent([new NumCoercible(10), 13, '12', true]), "union");
68AssertType(extentMixed, "union");
69AssertType(extent([new NumCoercible(10), 13, '12', true]), "union");
70AssertType(extent, "<T extends Numeric>((union)[]) => union");
71AssertType([new NumCoercible(10), 13, '12', true], "(union)[]");
72AssertType(new NumCoercible(10), "NumCoercible");
73AssertType(NumCoercible, "typeof NumCoercible");
74AssertType(10, "int");
75AssertType(13, "int");
76AssertType('12', "string");
77AssertType(true, "boolean");
78
79
80