You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functiontest(statement: string): string{constparts=statement.split('-');// <- parts will have at least one elementletfirstElement=parts.shift();// <- firstElement cannot be undefined at this pointif(parts.length>1){letsecondElement=parts.shift();// <- Still says that secondElement may be undefined!}}test('first-second');
Expected behavior:
String prototype split method always creates an array with at least one elements and therefore shift method cannot produce an undefined type.
Once we check the length of an array shift, pop cannot produce undefined type.
Actual behavior: shift, pop methods or accessing arrays indices return undefined even though the array has elements.
The text was updated successfully, but these errors were encountered:
There's not an easy way to communicate the probing you're doing with the return type of the methods using the type system today. You can convince the type system you know what you're doing with the non-null assertion operator (i.e. the ! operator):
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.5.2
Code
Expected behavior:
split
method always creates an array with at least one elements and thereforeshift
method cannot produce an undefined type.shift
,pop
cannot produce undefined type.Actual behavior:
shift
,pop
methods or accessing arrays indices returnundefined
even though the array has elements.The text was updated successfully, but these errors were encountered: