type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

Except no longer prevents the omitted keys to be keys present on the given type

Open nvdbleek opened this issue 2 years ago • 4 comments

The change for #263 in release v2.3.2 no longer prevents the use of the keys in the Except.

The following code no longer gives an error:

import { Except } from 'type-fest';

interface WithBar {
  foo: string;
  bar: number;
}

type WithoutBar = Except<WithBar, 'bar'>;

const withBar = { foo: 'Hell World', bar: 42 };
// The following assignment should result in an error, but no longer results in an error after the change of #263
const withoutBar: WithoutBar = withBar; 

Before 2.3.2 type-fest used never and you weren't allowed to assign an object that contains a key that was added to the except list. The documentation still states that Except is a stricter version of Omit, but I don't think it currently is.

Conceptually type-fest was generating the following code before 2.3.2:

interface WithBarNever {
  foo: string;
  bar: never;
}

// Results in a compile error as the object should not contain a `bar` property
const withoutBarNever: WithBarNever = withBar;

nvdbleek avatar Feb 07 '22 15:02 nvdbleek

I've created a codesandbox demonstrating the 'error' https://codesandbox.io/s/wizardly-hertz-wbv99?file=/src/index.ts

Line 12 doesn't contains a compile error (the old implementation used never and resulted in a compile error, see line 19 for this case)

nvdbleek avatar Feb 07 '22 15:02 nvdbleek

// @szmarczak

sindresorhus avatar Feb 07 '22 15:02 sindresorhus

I'm not able to reproduce the issue from the first comment:

https://www.typescriptlang.org/play?ts=4.7.4&ssl=11&ssc=1&pln=11&pc=41#code/KYDwDg9gTgLgBDAnmYcCiIDGwwwDwDyARgFbCYwAqywANHANLCIDO1KcoMwAdgCYs4Aa2YQAZnGJkK7YAD44AXjgAFAJaYhhUuSo16GTABsArn2B4RicZJ0z9jZmxpy5AbgBQHtT25QxAIbYcADqajAAFgBCAVBwAN4ecHBiEBAAXHAsMFA+AOaeyUSxmTwmALZEwFCeAL5eSBxhkRAmMDFxyoY4+M3RsfQA5MVQg+5emBA82XAA7uH9nQkpaZmDABLARkah0EZ8g-QjmQAsAExwtZ6T0-DzLW0dmX2t7bFKcwsdbnBAA

szmarczak avatar Aug 04 '22 13:08 szmarczak

The code you provided in codesandbox simplifies to

const withBar = { foo: "Hell World", bar: 42 };

interface WithBarNever {
  foo: string;
  bar: never;
}

const withoutBarNever: WithBarNever = withBar;

which doesn't use Except at all.

szmarczak avatar Aug 04 '22 13:08 szmarczak

I am closing this issue, it seems impossible to reproduce the error with the old or new version. Feel free to reopen this issue if you have a reproducible example.

skarab42 avatar Sep 15 '22 09:09 skarab42