tsdjs/tsd

Problems of checking optional object fields

MaksimLavrenyuk opened this issue · 1 comments

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

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