-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add numeric constraint to type parameter of mapped types with name ty…
…pe and array constraints
- Loading branch information
Showing
4 changed files
with
256 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
tests/baselines/reference/mappedTypeTupleConstraintTypeParameterInNameType.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
//// [tests/cases/compiler/mappedTypeTupleConstraintTypeParameterInNameType.ts] //// | ||
|
||
=== mappedTypeTupleConstraintTypeParameterInNameType.ts === | ||
// based on https://github.com/microsoft/TypeScript/issues/55762 | ||
|
||
declare class Decoder<T> { | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
>T : Symbol(T, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 2, 22)) | ||
|
||
decode(arrayBuffer: ArrayBuffer): T; | ||
>decode : Symbol(Decoder.decode, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 2, 26)) | ||
>arrayBuffer : Symbol(arrayBuffer, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 3, 9)) | ||
>ArrayBuffer : Symbol(ArrayBuffer, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 2, 22)) | ||
} | ||
|
||
type ValueTypeOf<T extends Decoder<any>> = T extends Decoder<infer R> | ||
>ValueTypeOf : Symbol(ValueTypeOf, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 4, 1)) | ||
>T : Symbol(T, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 6, 17)) | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
>T : Symbol(T, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 6, 17)) | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
>R : Symbol(R, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 6, 66)) | ||
|
||
? R | ||
>R : Symbol(R, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 6, 66)) | ||
|
||
: never; | ||
|
||
type StructDescriptor = ReadonlyArray< | ||
>StructDescriptor : Symbol(StructDescriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 8, 10)) | ||
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --)) | ||
|
||
readonly [key: string, type: Decoder<any>] | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
|
||
>; | ||
|
||
type StructTypeFor<Descriptor extends StructDescriptor> = { | ||
>StructTypeFor : Symbol(StructTypeFor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 12, 2)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 14, 19)) | ||
>StructDescriptor : Symbol(StructDescriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 8, 10)) | ||
|
||
[K in keyof Descriptor as Descriptor[K][0]]: ValueTypeOf<Descriptor[K][1]>; | ||
>K : Symbol(K, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 15, 3)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 14, 19)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 14, 19)) | ||
>K : Symbol(K, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 15, 3)) | ||
>ValueTypeOf : Symbol(ValueTypeOf, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 4, 1)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 14, 19)) | ||
>K : Symbol(K, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 15, 3)) | ||
|
||
}; | ||
|
||
class StructDecoder<const Descriptor extends StructDescriptor> extends Decoder< | ||
>StructDecoder : Symbol(StructDecoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 16, 2)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 18, 20)) | ||
>StructDescriptor : Symbol(StructDescriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 8, 10)) | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
|
||
StructTypeFor<Descriptor> | ||
>StructTypeFor : Symbol(StructTypeFor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 12, 2)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 18, 20)) | ||
|
||
> { | ||
constructor(descriptor: Descriptor) { | ||
>descriptor : Symbol(descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 21, 14)) | ||
>Descriptor : Symbol(Descriptor, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 18, 20)) | ||
|
||
super(); | ||
>super : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
} | ||
} | ||
|
||
declare const i32Decoder: Decoder<number>; | ||
>i32Decoder : Symbol(i32Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 26, 13)) | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
|
||
declare const i64Decoder: Decoder<bigint>; | ||
>i64Decoder : Symbol(i64Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 27, 13)) | ||
>Decoder : Symbol(Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 0, 0)) | ||
|
||
const structDecoder = new StructDecoder([ | ||
>structDecoder : Symbol(structDecoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 29, 5)) | ||
>StructDecoder : Symbol(StructDecoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 16, 2)) | ||
|
||
["a", i32Decoder], | ||
>i32Decoder : Symbol(i32Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 26, 13)) | ||
|
||
["b", i64Decoder], | ||
>i64Decoder : Symbol(i64Decoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 27, 13)) | ||
|
||
]); | ||
|
||
const struct = structDecoder.decode(new ArrayBuffer(100)); | ||
>struct : Symbol(struct, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 34, 5)) | ||
>structDecoder.decode : Symbol(Decoder.decode, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 2, 26)) | ||
>structDecoder : Symbol(structDecoder, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 29, 5)) | ||
>decode : Symbol(Decoder.decode, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 2, 26)) | ||
>ArrayBuffer : Symbol(ArrayBuffer, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
const v: number = struct.a; | ||
>v : Symbol(v, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 36, 5)) | ||
>struct.a : Symbol(a) | ||
>struct : Symbol(struct, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 34, 5)) | ||
>a : Symbol(a) | ||
|
||
const v2: bigint = struct.b; | ||
>v2 : Symbol(v2, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 37, 5)) | ||
>struct.b : Symbol(b) | ||
>struct : Symbol(struct, Decl(mappedTypeTupleConstraintTypeParameterInNameType.ts, 34, 5)) | ||
>b : Symbol(b) | ||
|
92 changes: 92 additions & 0 deletions
92
tests/baselines/reference/mappedTypeTupleConstraintTypeParameterInNameType.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
//// [tests/cases/compiler/mappedTypeTupleConstraintTypeParameterInNameType.ts] //// | ||
|
||
=== mappedTypeTupleConstraintTypeParameterInNameType.ts === | ||
// based on https://github.com/microsoft/TypeScript/issues/55762 | ||
|
||
declare class Decoder<T> { | ||
>Decoder : Decoder<T> | ||
|
||
decode(arrayBuffer: ArrayBuffer): T; | ||
>decode : (arrayBuffer: ArrayBuffer) => T | ||
>arrayBuffer : ArrayBuffer | ||
} | ||
|
||
type ValueTypeOf<T extends Decoder<any>> = T extends Decoder<infer R> | ||
>ValueTypeOf : ValueTypeOf<T> | ||
|
||
? R | ||
: never; | ||
|
||
type StructDescriptor = ReadonlyArray< | ||
>StructDescriptor : readonly (readonly [key: string, type: Decoder<any>])[] | ||
|
||
readonly [key: string, type: Decoder<any>] | ||
>; | ||
|
||
type StructTypeFor<Descriptor extends StructDescriptor> = { | ||
>StructTypeFor : StructTypeFor<Descriptor> | ||
|
||
[K in keyof Descriptor as Descriptor[K][0]]: ValueTypeOf<Descriptor[K][1]>; | ||
}; | ||
|
||
class StructDecoder<const Descriptor extends StructDescriptor> extends Decoder< | ||
>StructDecoder : StructDecoder<Descriptor> | ||
>Decoder : Decoder<StructTypeFor<Descriptor>> | ||
|
||
StructTypeFor<Descriptor> | ||
> { | ||
constructor(descriptor: Descriptor) { | ||
>descriptor : Descriptor | ||
|
||
super(); | ||
>super() : void | ||
>super : typeof Decoder | ||
} | ||
} | ||
|
||
declare const i32Decoder: Decoder<number>; | ||
>i32Decoder : Decoder<number> | ||
|
||
declare const i64Decoder: Decoder<bigint>; | ||
>i64Decoder : Decoder<bigint> | ||
|
||
const structDecoder = new StructDecoder([ | ||
>structDecoder : StructDecoder<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>new StructDecoder([ ["a", i32Decoder], ["b", i64Decoder],]) : StructDecoder<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>StructDecoder : typeof StructDecoder | ||
>[ ["a", i32Decoder], ["b", i64Decoder],] : [["a", Decoder<number>], ["b", Decoder<bigint>]] | ||
|
||
["a", i32Decoder], | ||
>["a", i32Decoder] : ["a", Decoder<number>] | ||
>"a" : "a" | ||
>i32Decoder : Decoder<number> | ||
|
||
["b", i64Decoder], | ||
>["b", i64Decoder] : ["b", Decoder<bigint>] | ||
>"b" : "b" | ||
>i64Decoder : Decoder<bigint> | ||
|
||
]); | ||
|
||
const struct = structDecoder.decode(new ArrayBuffer(100)); | ||
>struct : StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>structDecoder.decode(new ArrayBuffer(100)) : StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>structDecoder.decode : (arrayBuffer: ArrayBuffer) => StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>structDecoder : StructDecoder<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>decode : (arrayBuffer: ArrayBuffer) => StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>new ArrayBuffer(100) : ArrayBuffer | ||
>ArrayBuffer : ArrayBufferConstructor | ||
>100 : 100 | ||
|
||
const v: number = struct.a; | ||
>v : number | ||
>struct.a : number | ||
>struct : StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>a : number | ||
|
||
const v2: bigint = struct.b; | ||
>v2 : bigint | ||
>struct.b : bigint | ||
>struct : StructTypeFor<readonly [readonly ["a", Decoder<number>], readonly ["b", Decoder<bigint>]]> | ||
>b : bigint | ||
|
41 changes: 41 additions & 0 deletions
41
tests/cases/compiler/mappedTypeTupleConstraintTypeParameterInNameType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// based on https://github.com/microsoft/TypeScript/issues/55762 | ||
|
||
declare class Decoder<T> { | ||
decode(arrayBuffer: ArrayBuffer): T; | ||
} | ||
|
||
type ValueTypeOf<T extends Decoder<any>> = T extends Decoder<infer R> | ||
? R | ||
: never; | ||
|
||
type StructDescriptor = ReadonlyArray< | ||
readonly [key: string, type: Decoder<any>] | ||
>; | ||
|
||
type StructTypeFor<Descriptor extends StructDescriptor> = { | ||
[K in keyof Descriptor as Descriptor[K][0]]: ValueTypeOf<Descriptor[K][1]>; | ||
}; | ||
|
||
class StructDecoder<const Descriptor extends StructDescriptor> extends Decoder< | ||
StructTypeFor<Descriptor> | ||
> { | ||
constructor(descriptor: Descriptor) { | ||
super(); | ||
} | ||
} | ||
|
||
declare const i32Decoder: Decoder<number>; | ||
declare const i64Decoder: Decoder<bigint>; | ||
|
||
const structDecoder = new StructDecoder([ | ||
["a", i32Decoder], | ||
["b", i64Decoder], | ||
]); | ||
|
||
const struct = structDecoder.decode(new ArrayBuffer(100)); | ||
|
||
const v: number = struct.a; | ||
const v2: bigint = struct.b; |