镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Generic extending any becomes generic extending unknown #39471

Description

TypeScript Version: 3.9.2 and 4.0.0 beta (works fine on 3.8.2 and below)

Search Terms: generic extend any unknown

Expected behavior:
This code should compile just fine

Actual behavior:
I get an error saying Property 'error' does not exist on type 'T'. Because currentBaz: T extends unknown instead extends `any.

Related Issues:

Code

class Foo {
    public bar<T extends any>(baz: T[]): void {
        baz.forEach(currentBaz => {
            if (currentBaz.error) {
                console.log(currentBaz.error);
            }
        })
    }
}
Output
"use strict";
class Foo {
    bar(baz) {
        baz.forEach(currentBaz => {
            if (currentBaz.error) {
                console.log(currentBaz.error);
            }
        });
    }
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Activity

  1. IllusionMH commented on Jul 7, 2020

    @IllusionMH
    Contributor

    This is intended change Type Parameters That Extend any No Longer Act as any

    Also see discussion in that PR #29571

  2. gportela85 commented on Jul 7, 2020

    @gportela85
    Author
  3. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions