tsdjs / tsd

Check TypeScript type definitions
MIT License
2.36k stars 68 forks source link

Problems of checking optional object fields #172

Closed MaksimLavrenyuk closed 1 year ago

MaksimLavrenyuk commented 1 year ago

It seems that when checking objects with optional fields, unexpected behavior occurs. The objects fit the type, but expectType thinks they don't.

import { expectType } from 'tsd';

type FirstExample = {
    example: string | undefined
};

type SecondExample = {
    example?: string
};

expectType<FirstExample>({ example: 'first' });
expectType<SecondExample>({ example: 'second' });

The result of the work:

  ✖  Parameter type FirstExample is declared too wide for argument type { example: string; }.   
  ✖  Parameter type SecondExample is declared too wide for argument type { example: string; }.  

In typescript playground you can see that this is a valid type:

https://www.typescriptlang.org/play?#code/C4TwDgpgBAYglgJwM7AKIA8CGBbMAbaAXigG8BYAKCmqgi1wIC4oUE4A7AcygB8oBXdgBMIAMw4QhlAL4BuSpVCQoAZQgBjAPbCMOfEVKUatevoD8zVh04z5FSlvYoo45MGbw3uhgZIm9TFAA5K4oQVByDtrOSBraQoxqjkLe+lDEfnQBEMxBscnhckA

MaksimLavrenyuk commented 1 year ago

expectType - Strict type assertions, expectAssignable - t loose type assertion. I get it.