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

Question: implicit "any" from "yield" warning? #1715

Closed
Vanuan opened this issue Mar 10, 2020 · 7 comments
Closed

Question: implicit "any" from "yield" warning? #1715

Vanuan opened this issue Mar 10, 2020 · 7 comments
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin question Questions! (i.e. not a bug / enhancment / documentation)

Comments

@Vanuan
Copy link

Vanuan commented Mar 10, 2020

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

@Vanuan Vanuan added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Mar 10, 2020
@bradzacher
Copy link
Member

This will be handled more broadly by the no-unsafe-* set of rules I've been building.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party question Questions! (i.e. not a bug / enhancment / documentation) and removed triage Waiting for maintainers to take a look labels Mar 10, 2020
@Vanuan
Copy link
Author

Vanuan commented Mar 10, 2020

Awesome! Would it also handle unsafe array indexing?

@bradzacher
Copy link
Member

Yes, no-unsafe-member-access handles that as well.

@Vanuan
Copy link
Author

Vanuan commented Mar 10, 2020

Example:

const a: number[] = [];
const b: number | null = a[0] || null;
b.toFixed(); // runtime error, no type error

@Vanuan
Copy link
Author

Vanuan commented Mar 10, 2020

More complex example:

interface SimpleType { 
    id: string;
}
interface ComplexType {
    arrayField: SimpleType[];
}
const myObject = {
    arrayField: [],
}
const a: SimpleType[] = myObject.arrayField;
const b: SimpleType | null = a[0] || null;
b.id; // runtime error, no type error, should be Object is possibly 'null'.

@Vanuan
Copy link
Author

Vanuan commented Mar 10, 2020

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!

@Vanuan Vanuan closed this as completed Mar 10, 2020
@bradzacher
Copy link
Member

Ah that's what you meant by unsafe indexing.
No, there's no rule for that. That sort of thing ideally has to be built into typescript itself, as it requires control-flow analysis of the variable.

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
awaiting response Issues waiting for a reply from the OP or another party package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin question Questions! (i.e. not a bug / enhancment / documentation)
Projects
None yet
Development

No branches or pull requests

2 participants