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

Should Limit number based off if guard #56014

Closed
AndrewOttavianoAbb opened this issue Oct 6, 2023 · 2 comments
Closed

Should Limit number based off if guard #56014

AndrewOttavianoAbb opened this issue Oct 6, 2023 · 2 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@AndrewOttavianoAbb
Copy link

πŸ”Ž Search Terms

if guard for number

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about if guards.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAsiDKkDGBLAhgGwHIFcC2UAvFAAxQA+UAjBVAEy0DMA3AFCtID2AdgM7BQ8IAIIAnUUSgBtAEQpgEPFRkAaKHIV46q9fMWMZAXTYYIAoQDVMALlgJk6bPkkk2KAGZQAFELGiAdKbcAObAABZQADzEjACUUADerFCCIFYYkr7i-u4o3AAmAJIFEAAeXrxEAHxQlYT1upra8QDU1MxQAPSdUAAq4NAQ4pyirAC+rEA

πŸ’» Code

type MySpecialNum = 0 | 1 | 2 | 3;

const myArr = ["item1", "item2", "item3"];
let myVal: MySpecialNum = 0;
if (myArr.length <= 3) {
  myVal = myArr.findIndex(s => s === "item2") + 1; // Type error
}

πŸ™ Actual behavior

Type error on this line
myVal = myArr.findIndex(s => s === "item2") + 1; // Type error

πŸ™‚ Expected behavior

No type error.

I would expect no type error here because TypeScript has some really good type inference around if branches and typeof. This is really handy I would expect it to handle the problem above. Here is why I think TS should handle the case above:

  • I have declared my type to limit my number to the range 0-3 (which is another feature I'd love to see added)
  • Then I am creating a variable to find what position in my list a certain element is and I'm using my custom type
  • I have an if check to determine if I have less than 3 items in my array so that I can use findIndex to get the index of my element and add one to get the position in the list (note this still breaks if I remove the + 1 at the end

Ordinarily, one might say that findIndex could return something above my limiting type; that is true except I have an if statement guaranteeing that won't be a problem. In a sense, it is a kind of guard.

The main counter argument to this might be that we can't do anything about the arbitrary + 1 at the end. This is where my very limited knowledge of how TS works shows. I'm not sure if it would be possible to account for that, but I wouldn't think it impossible.

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds labels Oct 6, 2023
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Oct 6, 2023

This is at least two (probably more) feature requests in one: #26382, #54925 at a minimum.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Too Complex" 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 Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

3 participants