• 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/typeRelationships/subtypesAndSuperTypes/subtypingWithConstructSignatures5.ts ===
20declare function AssertType(value:any, type:string):void;
21
22// checking subtype relations for function types as it relates to contextual signature instantiation
23// same as subtypingWithConstructSignatures2 just with an extra level of indirection in the inheritance chain
24
25class Base { foo: string; }
26class Derived extends Base { bar: string; }
27class Derived2 extends Derived { baz: string; }
28class OtherDerived extends Base { bing: string; }
29
30interface A { // T
31    // M's
32    a: new (x: number) => number[];
33    a2: new (x: number) => string[];
34    a3: new (x: number) => void;
35    a4: new (x: string, y: number) => string;
36    a5: new (x: (arg: string) => number) => string;
37    a6: new (x: (arg: Base) => Derived) => Base;
38    a7: new (x: (arg: Base) => Derived) => (r: Base) => Derived;
39    a8: new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived;
40    a9: new (x: (arg: Base) => Derived, y: (arg2: Base) => Derived) => (r: Base) => Derived;
41    a10: new (...x: Derived[]) => Derived;
42    a11: new (x: { foo: string }, y: { foo: string; bar: string }) => Base;
43    a12: new (x: Array<Base>, y: Array<Derived2>) => Array<Derived>;
44    a13: new (x: Array<Base>, y: Array<Derived>) => Array<Derived>;
45    a14: new (x: { a: string; b: number }) => Object;
46}
47
48interface B extends A {
49    a: new <T>(x: T) => T[];
50}
51
52// S's
53interface I extends B {
54    // N's
55    a: new <T>(x: T) => T[]; // ok, instantiation of N is a subtype of M, T is number
56    a2: new <T>(x: T) => string[]; // ok
57    a3: new <T>(x: T) => T; // ok since Base returns void
58    a4: new <T, U>(x: T, y: U) => T; // ok, instantiation of N is a subtype of M, T is string, U is number
59    a5: new <T, U>(x: (arg: T) => U) => T; // ok, U is in a parameter position so inferences can be made
60    a6: new <T extends Base, U extends Derived>(x: (arg: T) => U) => T; // ok, same as a5 but with object type hierarchy
61    a7: new <T extends Base, U extends Derived>(x: (arg: T) => U) => (r: T) => U; // ok
62    a8: new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: T) => U) => (r: T) => U; // ok
63    a9: new <T extends Base, U extends Derived>(x: (arg: T) => U, y: (arg2: { foo: string; bing: number }) => U) => (r: T) => U; // ok, same as a8 with compatible object literal
64    a10: new <T extends Derived>(...x: T[]) => T; // ok
65    a11: new <T extends Base>(x: T, y: T) => T; // ok
66    a12: new <T extends Array<Base>>(x: Array<Base>, y: T) => Array<Derived>; // ok, less specific parameter type
67    a13: new <T extends Array<Derived>>(x: Array<Base>, y: T) => T; // ok, T = Array<Derived>, satisfies constraint, contextual signature instantiation succeeds
68    a14: new <T, U>(x: { a: T; b: U }) => T; // ok
69}
70
71