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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.Partial creates wrong type #139

Closed
iyegoroff opened this issue Aug 11, 2020 · 4 comments
Closed

Object.Partial creates wrong type #139

iyegoroff opened this issue Aug 11, 2020 · 4 comments
Assignees
Labels
bug Something isn't working wontfix This will not be worked on
Projects

Comments

@iyegoroff
Copy link

馃悶 Bug Report

Describe the bug

The type created by Object.Partial is weaker than expected

Reproduce the bug

type T = { x: { y: never; z: () => 1 } }
// No error
const err: Object.Partial<T, 'deep'> = { x: { z: () => 'test' } }

Expected behavior

tsc should produce an error:

(property) z?: (() => 1) | undefined
Type '() => string' is not assignable to type '() => 1'.
  Type 'string' is not assignable to type '1'.ts(2322)

Possible Solution

type DeepPartial<T> = T extends Record<string, unknown>
  ? Partial<{ [Key in keyof T]: DeepPartial<T[Key]> }>
  : T

// Error: ...  Type 'string' is not assignable to type '1'.ts(2322)
const ok: DeepPartial<T> = { x: { z: () => 'test' } }
@millsp millsp self-assigned this Aug 11, 2020
@millsp millsp added the bug Something isn't working label Aug 11, 2020
@millsp millsp added this to To do in Board via automation Aug 11, 2020
@millsp
Copy link
Owner

millsp commented Aug 11, 2020

Heya @iyegoroff, thanks for reporting this. It's fixed. It will be out in a few minutes. Thanks!

@millsp millsp closed this as completed Aug 11, 2020
Board automation moved this from To do to Done Aug 11, 2020
@iyegoroff
Copy link
Author

Looks like it is broken again in v9.6.0

@millsp millsp reopened this May 26, 2021
Board automation moved this from Done to In progress May 26, 2021
@hjkcai
Copy link

hjkcai commented Jul 1, 2021

I have found another broken case:

Playground Link

import { O } from 'ts-toolbelt'

type A = { foo: unknown }
type B = O.Partial<A, 'deep'>

declare const a: A
const b: B = a

The code above results the following error:

Type 'A' is not assignable to type 'OptionalDeep<A>'.
  Types of property 'foo' are incompatible.
    Type 'unknown' is not assignable to type 'OptionalDeep<unknown> | undefined'.
      Type 'unknown' is not assignable to type 'OptionalDeep<unknown>'.(2322)

Normally unknown can never be passed into OptionalDeep because of the constraint T extends object. However when unknown is a value type of an object, it bypassed the constraint, resulting incorrect type OptionalDeep<unknown>.

IMO unknown may need to have special treatment to avoid this pitfall.

@stale
Copy link

stale bot commented Sep 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Sep 6, 2021
@stale stale bot closed this as completed Sep 21, 2021
Board automation moved this from In progress to Done Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working wontfix This will not be worked on
Projects
Board
  
Done
Development

No branches or pull requests

3 participants