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

No report of implicit any type in a generator when destructuring #43172

Open
Igorbek opened this issue Mar 10, 2021 · 5 comments
Open

No report of implicit any type in a generator when destructuring #43172

Igorbek opened this issue Mar 10, 2021 · 5 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Igorbek
Copy link
Contributor

Igorbek commented Mar 10, 2021

Bug Report

πŸ”Ž Search Terms

no implicit any, generator, yield, destructure
#41348 - Report implicit any error for 'yield' result with no contextual type

πŸ•— Version & Regression Information

TypeScript 4.2.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function* sample() {
    const a = yield 1 // error, as expected, a is implicitly any
    const { b } = yield 1 // no error, but expected, b is implicitly any
    const [c] = yield 1 // no error, but expected, c is implicitly any
}

πŸ™ Actual behavior

b, c implicitly have type any, but no error reported

πŸ™‚ Expected behavior

Errors reported

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 10, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 10, 2021
@RyanCavanaugh RyanCavanaugh added the Help Wanted You can do this label Mar 10, 2021
@a-tarasyuk
Copy link
Contributor

@RyanCavanaugh What about the following case?

function* g() {
    const [a = 1, b = 2] = yield; ???
}

@Igorbek
Copy link
Contributor Author

Igorbek commented Mar 11, 2021

@a-tarasyuk it seems they're given context types, so it should not be an error. However, the intention might be:

const [a = 1, b = 2]: [number | string, number | boolean] = yield;

not really definitive.

@a-tarasyuk
Copy link
Contributor

Should the expected behavior be like this?

function* g1() {
    const { a } = yield;                 // error: implicit any
    const { b }: { b: string } = yield;  // ok
    const { c = 1 } = yield;             // ok ??
}

function* g2() {
    const [a] = yield;           // error: implicit any
    const [b]: [string] = yield; // ok
    const [c = 1] = yield;       //  ok ??
}

/cc @DanielRosenwasser @RyanCavanaugh

@Igorbek
Copy link
Contributor Author

Igorbek commented Mar 11, 2021

I think you're correct with inferred type from the default being ok, as it'd be consistent with plain functions:

function f2({ a }) {} // error
function f2({ a }: { a: number }) {} // ok
function f3({a = 1}) {} // ok

@Igorbek
Copy link
Contributor Author

Igorbek commented Mar 11, 2021

ah, on the second thought, if it is const binding, the type is not widening, which is not the same in the argument destructuring. You either end up having a type of const context or have inconsistency and widen the type even in const binding.
I'd say in this case it should be an error then. Although let would work just fine, for consistency, I'd also vote for the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants