• 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/conformance/types/intersection/operatorsAndIntersectionTypes.ts ===
20declare function AssertType(value:any, type:string):void;
21type Guid = string & { $Guid };          // Tagged string type
22type SerialNo = number & { $SerialNo };  // Tagged number type
23
24function createGuid() {
25AssertType("21EC2020-3AEA-4069-A2DD-08002B30309D" as Guid, "Guid");
26AssertType("21EC2020-3AEA-4069-A2DD-08002B30309D", "string");
27    return "21EC2020-3AEA-4069-A2DD-08002B30309D" as Guid;
28}
29
30function createSerialNo() {
31AssertType(12345 as SerialNo, "SerialNo");
32AssertType(12345, "int");
33    return 12345 as SerialNo;
34}
35
36let map1: { [x: string]: number } = {};
37AssertType(map1, "{ [string]: number; }");
38AssertType(x, "string");
39AssertType({}, "{}");
40
41let guid = createGuid();
42AssertType(guid, "Guid");
43AssertType(createGuid(), "Guid");
44AssertType(createGuid, "() => Guid");
45
46map1[guid] = 123;  // Can with tagged string
47AssertType(map1[guid] = 123, "int");
48AssertType(map1[guid], "number");
49AssertType(map1, "{ [string]: number; }");
50AssertType(guid, "Guid");
51AssertType(123, "int");
52
53let map2: { [x: number]: string } = {};
54AssertType(map2, "{ [number]: string; }");
55AssertType(x, "number");
56AssertType({}, "{}");
57
58let serialNo = createSerialNo();
59AssertType(serialNo, "SerialNo");
60AssertType(createSerialNo(), "SerialNo");
61AssertType(createSerialNo, "() => SerialNo");
62
63map2[serialNo] = "hello";  // Can index with tagged number
64AssertType(map2[serialNo] = "hello", "string");
65AssertType(map2[serialNo], "string");
66AssertType(map2, "{ [number]: string; }");
67AssertType(serialNo, "SerialNo");
68AssertType("hello", "string");
69
70const s1 = "{" + guid + "}";
71AssertType(s1, "string");
72AssertType("{" + guid + "}", "string");
73AssertType("{" + guid, "string");
74AssertType("{", "string");
75AssertType(guid, "Guid");
76AssertType("}", "string");
77
78const s2 = guid.toLowerCase();
79AssertType(s2, "string");
80AssertType(guid.toLowerCase(), "string");
81AssertType(guid.toLowerCase, "() => string");
82
83const s3 = guid + guid;
84AssertType(s3, "string");
85AssertType(guid + guid, "string");
86AssertType(guid, "Guid");
87AssertType(guid, "Guid");
88
89const s4 = guid + serialNo;
90AssertType(s4, "string");
91AssertType(guid + serialNo, "string");
92AssertType(guid, "Guid");
93AssertType(serialNo, "SerialNo");
94
95const s5 = serialNo.toPrecision(0);
96AssertType(s5, "string");
97AssertType(serialNo.toPrecision(0), "string");
98AssertType(serialNo.toPrecision, "(?number) => string");
99AssertType(0, "int");
100
101const n1 = serialNo * 3;
102AssertType(n1, "number");
103AssertType(serialNo * 3, "number");
104AssertType(serialNo, "SerialNo");
105AssertType(3, "int");
106
107const n2 = serialNo + serialNo;
108AssertType(n2, "number");
109AssertType(serialNo + serialNo, "number");
110AssertType(serialNo, "SerialNo");
111AssertType(serialNo, "SerialNo");
112
113const b1 = guid === "";
114AssertType(b1, "boolean");
115AssertType(guid === "", "boolean");
116AssertType(guid, "Guid");
117AssertType("", "string");
118
119const b2 = guid === guid;
120AssertType(b2, "boolean");
121AssertType(guid === guid, "boolean");
122AssertType(guid, "Guid");
123AssertType(guid, "Guid");
124
125const b3 = serialNo === 0;
126AssertType(b3, "boolean");
127AssertType(serialNo === 0, "boolean");
128AssertType(serialNo, "SerialNo");
129AssertType(0, "int");
130
131const b4 = serialNo === serialNo;
132AssertType(b4, "boolean");
133AssertType(serialNo === serialNo, "boolean");
134AssertType(serialNo, "SerialNo");
135AssertType(serialNo, "SerialNo");
136
137
138