Skip to content

expectTypeOf allows an incorrect any value instead of the specified type  #4205

@kwangure

Description

@kwangure

Describe the bug

When I use expectTypeOf, I expect it to fail when I pass any as input. For example, toBeString() should strictly only allow strings and not any other type...including any.

expectTypeOf(1).toBeString(); //  Fails 👍
expectTypeOf(1 as any).toBeString(); // Passes 👎

This is particular useful in cases when your types reason about any, I need to strictly know that a type will fail if its any or not. For example:

type IsAny<T> = 0 extends 1 & T ? true : false;

export type CustomType<T> = IsAny<T> extends true ? number: any;

If I use CustomType somewhere, I'd like to test expectTypeOf(value).toBeNumber(); and have it fail if it's any when I don't expect it to be.

Reproduction

expectTypeOf(1).toBeString(); //  Fails 👍
expectTypeOf(1 as any).toBeString(); // Passes 👎

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.2 LTS 22.04.2 LTS (Jammy Jellyfish)
    CPU: (4) x64 Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
    Memory: 683.74 MB / 3.69 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 20.4.0 - ~/.nvm/versions/node/v20.4.0/bin/node
    npm: 9.7.2 - ~/.nvm/versions/node/v20.4.0/bin/npm
    pnpm: 8.7.1 - ~/.nvm/versions/node/v20.4.0/bin/pnpm
  npmPackages:
    vite: ^4.4.8 => 4.4.9 
    vitest: ^0.33.0 => 0.33.0

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions