-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Confusing 'never' return type when appending an empty array #9976
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
Comments
Maybe there Is there something like |
@basarat it is called not setting |
#8944 covers why this is done |
@RyanCavanaugh I've read through the related discussions and now I get the logic of why |
My question: why never in this: let rows: Element[] = [].concat(generateElements()); but not in this: let rows: Element[] = [];
rows.concat(generateElements()); |
@108adams - Your code example doesn't actually concat the rows. As concat returns a new array. By doing You'd need to say:
|
I love how Typescript is still giving me an error:
When I've clearly annotated the type: export function getTilesInSection(
x: number,
y: number,
sectionWidth: number,
sectionHeight: number,
tileSize: number
): GridTileContainer[] {
const halfSectionWidth = sectionWidth / 2;
const halfSectionHeight = sectionHeight / 2;
const halfTileSize = tileSize / 2;
const x1 = x - halfSectionWidth;
const x2 = x + halfSectionWidth;
const y1 = y - halfSectionHeight;
const y2 = y + halfSectionHeight;
const xMidpoints = multiplesInRange(tileSize, x1, x2);
const yMidpoints = multiplesInRange(tileSize, y1, y2);
let containers: GridTileContainer[] = [];
for (const x of xMidpoints) {
for (const y of yMidpoints) {
containers.push({
style: {
position: "absolute",
x: x - halfTileSize,
y: y - halfTileSize,
height: tileSize,
width: tileSize
}
});
}
}
return containers;
} I don't see anything wrong with that, and no errors display in VS Code, yet it won't compile. 😕 |
@TAGC I tried your code in TypeScript 2.9.1 and don't hit the |
@robertpenner I think it's because that was in a Vue file. I've found out that there's a few issues in integrating Vue with TypeScript. Edit: scratch that, it's unlikely that was in a Vue component file, since I don't need to export functions in those. Although I was probably consuming that function within a Vue component and maybe that's what caused the issue. |
I still have question on this: [].concat(a, b); // <- not good
Array().concat(a, b) // <- okay I have a use case that I'm using this workaround since |
why type error const nextList = list.concat(parseInt(number)); |
Here is a short list of every way I can think of writing this... let foo: string[] = ['one', 'two', 'three'];
// Bad - this will throw a compile error.
let one: string[] = [].concat(...foo);
// Good
let two: string[] = Array().concat(...foo);
// Good
let three: string[] = [];
three.concat(...foo);
// Good
let four: string[] = ([] as string[]).concat(...foo);
// Good - but throws an error in the playground because it thinks this the type annotation is JSX.
// You must set JSX to "none" to remove the error.
let four: string[] = (<string[]>[]).concat(...foo); |
TypeScript Version: 2.0.0
Code
Expected behavior:
Accept this, and have the type inferencer postulate
x:number[]
. Alternatively, an error message along the lines of "cannot assignnumber
to parameter{}
" (because type inferencer could also assumex:undefined[]
).Actual behavior:
(with
strictNullCheck
)error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
This might be because of how
never
is documented as the "bottom" type (for functions that never return), but since the code isn't using the result expression, this is pretty confusing. Is this because the assumed type ofx
isnever[]
?I tried looking for related issues, I feel like it's slightly related to empty tuple discussions, but not 100% sure.
The text was updated successfully, but these errors were encountered: