Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve apparent type of mapped types #57122

Merged
merged 9 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
assignmentToAnyArrayRestParameters.ts(15,25): error TS2339: Property '0.0' does not exist on type 'string[]'.
assignmentToAnyArrayRestParameters.ts(18,16): error TS2536: Type '"0.0"' cannot be used to index type 'T'.


==== assignmentToAnyArrayRestParameters.ts (2 errors) ====
// Repros from #57122

function foo<T extends string[]>(
fa: (s: string, ...args: string[]) => string,
fb: (s: string, ...args: T) => string
) {
const f1: (...args: any) => string = fa;
const f2: (...args: any[]) => string = fa;
const f3: (...args: any) => string = fb;
const f4: (...args: any[]) => string = fb;
}

function bar<T extends string[], K extends number>() {
type T00 = string[]["0"];
type T01 = string[]["0.0"]; // Error
~~~~~
!!! error TS2339: Property '0.0' does not exist on type 'string[]'.
type T02 = string[][K | "0"];
type T10 = T["0"];
type T11 = T["0.0"]; // Error
~~~~~~~~
!!! error TS2536: Type '"0.0"' cannot be used to index type 'T'.
type T12 = T[K | "0"];
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/assignmentToAnyArrayRestParameters.ts] ////

=== assignmentToAnyArrayRestParameters.ts ===
// Repro from #57122
// Repros from #57122

function foo<T extends string[]>(
>foo : Symbol(foo, Decl(assignmentToAnyArrayRestParameters.ts, 0, 0))
Expand Down Expand Up @@ -40,3 +40,32 @@ function foo<T extends string[]>(
>fb : Symbol(fb, Decl(assignmentToAnyArrayRestParameters.ts, 3, 49))
}

function bar<T extends string[], K extends number>() {
>bar : Symbol(bar, Decl(assignmentToAnyArrayRestParameters.ts, 10, 1))
>T : Symbol(T, Decl(assignmentToAnyArrayRestParameters.ts, 12, 13))
>K : Symbol(K, Decl(assignmentToAnyArrayRestParameters.ts, 12, 32))

type T00 = string[]["0"];
>T00 : Symbol(T00, Decl(assignmentToAnyArrayRestParameters.ts, 12, 54))

type T01 = string[]["0.0"]; // Error
>T01 : Symbol(T01, Decl(assignmentToAnyArrayRestParameters.ts, 13, 29))

type T02 = string[][K | "0"];
>T02 : Symbol(T02, Decl(assignmentToAnyArrayRestParameters.ts, 14, 31))
>K : Symbol(K, Decl(assignmentToAnyArrayRestParameters.ts, 12, 32))

type T10 = T["0"];
>T10 : Symbol(T10, Decl(assignmentToAnyArrayRestParameters.ts, 15, 33))
>T : Symbol(T, Decl(assignmentToAnyArrayRestParameters.ts, 12, 13))

type T11 = T["0.0"]; // Error
>T11 : Symbol(T11, Decl(assignmentToAnyArrayRestParameters.ts, 16, 22))
>T : Symbol(T, Decl(assignmentToAnyArrayRestParameters.ts, 12, 13))

type T12 = T[K | "0"];
>T12 : Symbol(T12, Decl(assignmentToAnyArrayRestParameters.ts, 17, 24))
>T : Symbol(T, Decl(assignmentToAnyArrayRestParameters.ts, 12, 13))
>K : Symbol(K, Decl(assignmentToAnyArrayRestParameters.ts, 12, 32))
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//// [tests/cases/compiler/assignmentToAnyArrayRestParameters.ts] ////

=== assignmentToAnyArrayRestParameters.ts ===
// Repro from #57122
// Repros from #57122

function foo<T extends string[]>(
>foo : <T extends string[]>(fa: (s: string, ...args: string[]) => string, fb: (s: string, ...args: T) => string) => void
Expand Down Expand Up @@ -38,3 +38,25 @@ function foo<T extends string[]>(
>fb : (s: string, ...args: T) => string
}

function bar<T extends string[], K extends number>() {
>bar : <T extends string[], K extends number>() => void

type T00 = string[]["0"];
>T00 : string

type T01 = string[]["0.0"]; // Error
>T01 : any

type T02 = string[][K | "0"];
>T02 : string[][K | "0"]

type T10 = T["0"];
>T10 : T["0"]

type T11 = T["0.0"]; // Error
>T11 : T["0.0"]

type T12 = T[K | "0"];
>T12 : T[K | "0"]
}

11 changes: 10 additions & 1 deletion tests/cases/compiler/assignmentToAnyArrayRestParameters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @strict: true
// @noEmit: true

// Repro from #57122
// Repros from #57122

function foo<T extends string[]>(
fa: (s: string, ...args: string[]) => string,
Expand All @@ -12,3 +12,12 @@ function foo<T extends string[]>(
const f3: (...args: any) => string = fb;
const f4: (...args: any[]) => string = fb;
}

function bar<T extends string[], K extends number>() {
type T00 = string[]["0"];
type T01 = string[]["0.0"]; // Error
type T02 = string[][K | "0"];
type T10 = T["0"];
type T11 = T["0.0"]; // Error
type T12 = T[K | "0"];
}
Comment on lines +16 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: those should likely be in a different test file since they don't have anything in common with this "test title" (assignmentToAnyArrayRestParameters)

Loading