Skip to content
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

Except is not strict #556

Closed
nurliman opened this issue Feb 17, 2023 · 4 comments · Fixed by #560
Closed

Except is not strict #556

nurliman opened this issue Feb 17, 2023 · 4 comments · Fixed by #560

Comments

@nurliman
Copy link

nurliman commented Feb 17, 2023

import { Except } from "type-fest";

type Foo = {
    a: number;
    b: string;
    c: boolean;
};

type FooWithoutA = Except<Foo, 'a'>;

const foo: Foo = {
    a: 1,
    b: 'b',
    c: true
}

const fooWithoutA: FooWithoutA = foo; // not error

playground link

Is this intended behavior? if yes, how can i restrict foo for being assign to fooWithoutA?
Thank you.

@orimiles5
Copy link
Contributor

This is probably because TypeScript has a design decision that causes it to only check for excess properties when an object is defined using an object literal syntax.

It is possible to restrict foo for being assign to fooWithoutA with this solution:

import { Except } from "type-fest";

type StrictExcept<T, K extends keyof T> = Except<T, K> & Partial<Record<K, never>>;

type Foo = {
    a: number;
    b: string;
    c: boolean;
};

type FooWithoutA = StrictExcept<Foo, 'a'>;

const foo: Foo = {
    a: 1,
    b: 'b',
    c: true
}

const fooWithoutA: FooWithoutA = foo; // error

If relevant, I would be happy to submit a PR and fix the original Except.

@nurliman
Copy link
Author

This is probably because TypeScript has a design decision that causes it to only check for excess properties when an object is defined using an object literal syntax.

It is possible to restrict foo for being assign to fooWithoutA with this solution:

import { Except } from "type-fest";

type StrictExcept<T, K extends keyof T> = Except<T, K> & Partial<Record<K, never>>;

type Foo = {
    a: number;
    b: string;
    c: boolean;
};

type FooWithoutA = StrictExcept<Foo, 'a'>;

const foo: Foo = {
    a: 1,
    b: 'b',
    c: true
}

const fooWithoutA: FooWithoutA = foo; // error

If relevant, I would be happy to submit a PR and fix the original Except.

Cool, your solution worked. Thank you!

@tommy-mitchell
Copy link
Contributor

It is possible to restrict foo for being assign to fooWithoutA with this solution:

// ...

If relevant, I would be happy to submit a PR and fix the original Except.

@orimiles5 Maybe adding an ExceptOptions similar to Get would suffice?

type GetOptions = {
/**
Include `undefined` in the return type when accessing properties.
Setting this to `false` is not recommended.
@default true
*/
strict?: boolean;
};

The default behavior would be the updated strict version, and users could opt in to the non-strict Except if they were relying on the old behavior.

@orimiles5
Copy link
Contributor

It is possible to restrict foo for being assign to fooWithoutA with this solution:

// ...

If relevant, I would be happy to submit a PR and fix the original Except.

@orimiles5 Maybe adding an ExceptOptions similar to Get would suffice?

type GetOptions = {
/**
Include `undefined` in the return type when accessing properties.
Setting this to `false` is not recommended.
@default true
*/
strict?: boolean;
};

The default behavior would be the updated strict version, and users could opt in to the non-strict Except if they were relying on the old behavior.

Great idea. I’ll work on it. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants