Skip to content

number extends Enum should evaluate to false #43269

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

Closed
ljqx opened this issue Mar 16, 2021 · 2 comments
Closed

number extends Enum should evaluate to false #43269

ljqx opened this issue Mar 16, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ljqx
Copy link

ljqx commented Mar 16, 2021

TypeScript Version: 4.2.3

Search Terms: number extends enum

Code

export enum NumericEnumFruit {
  APPLE = 1,
  BANANA = 2,
}

export type NumericEnumFruitIsSuperNumber = number extends NumericEnumFruit ? NumericEnumFruit : never; // Yes, returns NumericEnumFruit
export type NumericEnumFruitIsSuperString = string extends NumericEnumFruit ? NumericEnumFruit : never; // No, returns never

export enum StringEnumFruit {
  APPLE = 'apple',
  BANANA = 'banana',
}

export type StringEnumFruitIsSuperNumber = number extends StringEnumFruit ? StringEnumFruit : never; // No, returns never
export type StringEnumFruitIsSuperString = string extends StringEnumFruit ? StringEnumFruit : never; // No, returns never

export enum HeterogeneousEnumFruit {
  APPLE = 'apple',
  BANANA = 2,
}

export type HeterogeneousEnumFruitIsSuperNumber = number extends HeterogeneousEnumFruit ? HeterogeneousEnumFruit : never; // Yes, returns HeterogeneousEnumFruit
export type HeterogeneousEnumFruitIsSuperString = string extends HeterogeneousEnumFruit ? HeterogeneousEnumFruit : never; // No, returns never

const ConstObjectFruit = {
    APPLE: 'apple',
    BANANA: 2,
    // 2: 'BANANA'
} as const;
type ConstObjectFruitType = typeof ConstObjectFruit[keyof typeof ConstObjectFruit];

export type ConstObjectFruitIsSuperNumber = number extends ConstObjectFruitType ? ConstObjectFruitType : never; // No, returns never
export type ConstObjectFruitIsSuperString = string extends ConstObjectFruitType ? ConstObjectFruitType : never; // No, returns never

Expected behavior:
Always got never for all above conditional types.

Actual behavior:
For number extends Enum cases, it's evaluated to true, instead of false. While things are correct for string extends Enum and number extends ConstObjectType.

Playground Link:
https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBMAdgVwLZwHJuFAlgYwFEVUAxKZXeAbwCg44BBABWYBlC4BeOARgBp6cAEKMMYxtzgAmQQF9atUJFhwYATzDBM2PERLlKMAJIBnAMrItULKgBGOKSQdQEIGEgAmpnahwFiNEMqOAB+X399IIoQgC44RGAANxwAbjgAegy4AE1gU344KGAYZChEH1tIwLIYmCVwaHgNLQi9GuCTCysccxg8RABzKVN+3CG3D0RvNoCDOrDZqNqjOHjElKh0rMwIQuLS8p8NnEVlJoQSOD6BwY6FugYWdk4eAHIAQzAwABtgN8EDFE4nEUjedg+iEhHwBtAUDRUzU02hu4zu8yMZks1lsLicaDxoCmM1RQ3uq3CpPR0VW62SaUy2QweyKJTKFQS9KgCIuLRRYzJGKoWJ6UCpIwFwyJXh8VPJIUpkvl8Dpm22TJZB3Zxy5Z0aqiQaDgAAkSjgIIMkMAIMhTMq4I8mKwOGCvr9-oCRBJQTxZHC9Yi1MiTWaoBarTa7UKuticLjHDxnI5pdMfKaPGHLYlI-bwunzVnrbb7aqGTs8gVWYcOfnMxHi9Geao+SGM+Hsw2acLutZxTxRrdJjLWwX61Gu-A86H20Xxys4py1Yzdvs2UdF6daPgIBV4ABhHejADydgAVsB8DBOlJHU9nYR4p9vn9YQwgd7GPE-W-l9JH8CJDeOE4A+Hxt13VJaBbA9dxPc9L06AAVYMeD5CAADM4Bg48zwvK86gAbQAa2AdQMKDLRyOwmA4LwzoAF1IKbJFWmo2iELqEUcQJBMEh41wUxmNjcI4oxkNacJhPg-CxODUstmXZlV2rHVNmYijtCkujOJ7XpJQlQdBJ8LTRKocTtEkw8aJEmSzLkjcFJ2JSq21BzaCAA

Related Issues:
Didn't find related issues.

@MartinJohns
Copy link
Contributor

See: #32690 (comment)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 16, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants