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
Question: implicit "any" from "yield" warning? #1715
Comments
This will be handled more broadly by the
|
Awesome! Would it also handle unsafe array indexing? |
Yes, no-unsafe-member-access handles that as well. |
Example:
|
More complex example:
|
Well, it looks like the latter is a bug in TypeScript. The type error is working for primitive type, but not for user-defined types. Closing the issue as it's a different question. Thank you! |
Ah that's what you meant by unsafe indexing. You could also encode this in the types, and ban member expressions on non-tuple arrays. function hasLength<T>(n: 1, arr: T[]): arr is [T, ...T[]];
function hasLength<T>(n: 2, arr: T[]): arr is [T, T, ...T[]];
function hasLength<T>(n: 3, arr: T[]): arr is [T, T, T, ...T[]];
// ....
function hasLength<T>(n: number, arr: T[]): arr is T[] {
return arr.length === n;
};
declare const x: number[];
const y = hasLength(2, x); // typeof y === [number, number, ...number[]];
x[0] // hypothetical-lint-rule-error: No array index access, use hasLength to validate the length first
y[0] // no error, as it's provably safe |
Is it feasible to add a warning for yield result to be assigned to non-typed variable or as non-typed argument?
TypeScript doesn't always infer the yield return type:
microsoft/TypeScript#26959
The text was updated successfully, but these errors were encountered: