Skip to content

| undefined should be part of type when getting items from an array #38470

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

Closed
lonewarrior556 opened this issue May 11, 2020 · 7 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@lonewarrior556
Copy link

lonewarrior556 commented May 11, 2020

Search Terms: Array type

Code

Simplest code:

var a: string[] = ['a'];

var shouldBeString = a[0] // string 
var couldBeString = a[10000]; // string 
var notAString = a[Infinity]; // string 
var alsoNotAString = a[NaN]; // string 

I get the difficulty in discerning 0, 10, NaN and Infinity since the type of all those is number.. but wouldn't it make sense then to make the return type from these queries string | undefined?
For those who don't want that they can turn off strictNullChecks

Tuples are only slightly better

// tuple 
var str = '';
var aa = [str, str] as const

var _shouldBeString = aa[0] //string
var _notString1 = aa[10000]; // type error
var _notString2 = aa[Infinity]; // no error
var _notString3 = aa[NaN]; // no error

Expected behavior:

var arr = [''];
var item = arr[10];
// typeof item = string | undefined;

Actual behavior:

var arr = [''];
var item = arr[10];
// typeof item = string;

Playground Link:
TS-Playground

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 11, 2020
@MartinJohns
Copy link
Contributor

Duplicate of #13778.

@RyanCavanaugh
Copy link
Member

Duplicate #13778

@RyanCavanaugh
Copy link
Member

@MartinJohns easy dupe day eh

@MartinJohns
Copy link
Contributor

@RyanCavanaugh I spend way too much time reading these issues in this repository, for quite some time already (using TypeScript since preview version), so I often recognize repeated issues. Just sometimes I fail to find the original issue.

@lonewarrior556
Copy link
Author

lonewarrior556 commented May 11, 2020

@RyanCavanaugh @MartinJohns
Fine, yes part one complete overlap..

However I did not see this tuple behavior being address in the other thread.. Can't see how those arguments would apply here:

// tuple 
var str = '';
var aa = [str, str] as const

var _shouldBeString = aa[0] //string
var _notString1 = aa[10000]; // type error
var _notString2 = aa[Infinity]; // no error, type string
var _notString3 = aa[NaN]; // no error, type string

@RyanCavanaugh
Copy link
Member

Infinity and NaN aren't specially typed; they're just number. See #32277 and #28682

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants