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

Given Array<T> const index which not overflow, should indicate element type T instead of T|undefined #52842

Closed
loynoir opened this issue Feb 18, 2023 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@loynoir
Copy link

loynoir commented Feb 18, 2023

Bug Report

🔎 Search Terms

  • Array<T>
  • const integer index
  • not overflow index
  • indicate element type from array and array index
  • array element type might be undefined

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

💻 Code

function fn(s: string) {
        console.log(s)
}

function reproduce(input: string[]) {
        switch (reproduce.length) {
                case 0 as const:
                case 1 as const:
                        fn(input[0 as const])
                        break
                default:
                        break
        }
}

const input: string[] = []
switch (input.length) {
        case 0 as const:
        case 1 as const:
                fn(input[0 as const])
                break
        default:
                break
}

🙁 Actual behavior

// fn(input[0 as const])
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

🙂 Expected behavior

// fn(input[0 as const])
OK

Additional

I guess there is already an issue.

But seems not easy to find which to track on from tons of issues.

@MartinJohns
Copy link
Contributor

MartinJohns commented Feb 18, 2023

Duplicate of Related #38000 (misunderstood what you wanted). Used search terms: array length in:title

Given the example I really don't see any purpose in this. It's easier and cleaner to just access the element and check for undefined.

@loynoir
Copy link
Author

loynoir commented Feb 18, 2023

Background

@MartinJohns

The purpose is parsing column base text into interface.

The column base text definition is in linux kernel.

The column is generated from str.split.

Some columns are required, some columns are optional, based on columns length.

Given

  • columns is Array<string> or string[]
  • casting columns into interface
  • given const-not-overflow-index

Actual

  • columns[index] is T|undefined

Expected

  • columns[index] is T

Workaround

  • columns is LengthCaseArray<string>
// prettier-ignore
type LengthCaseArray<T> =
|[] & {length:0}
|[T] & {length:1}
|[T,T] & {length:2}
|[T,T,T] & {length:3}
|[T,T,T,T] & {length:4}
|[T,T,T,T,T] & {length:5}
|[T,T,T,T,T,T] & {length:6}
|[T,T,T,T,T,T,T] & {length:7}
|[T,T,T,T,T,T,T,T] & {length:8}
|[T,T,T,T,T,T,T,T,T] & {length:9}
|[T,T,T,T,T,T,T,T,T,T] & {length:10}
|[T,T,T,T,T,T,T,T,T,T,T] & {length:11}
|[T,T,T,T,T,T,T,T,T,T,T,T] & {length:12}
|[T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:13}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:14}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:15}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:16}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:17}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:18}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:19}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:20}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:21}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:22}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:23}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:24}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:25}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:26}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:27}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:28}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:29}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:30}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T] & {length:31}
|[T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,T,...T[]] & {length:number}

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Feb 23, 2023
@RyanCavanaugh
Copy link
Member

This was a well-defined limitation in #39560. We're offering two options here:

  • noUncheckedIndexedAccess off, which is convenient in all cases
  • noUncheckedIndexedAccess on, which is sound in all cases

Given that we can't fully track all possible array mutations, we don't intend to add a third in-between option, nor unsound behavior in the latter option.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants