Skip to content

Regression: Partial properties became undefined instead of optional #37324

Closed

Description

TypeScript Version: 3.9.0-dev.20200310

Search Terms:
Partial

Code

export interface Messages {
  [key: string]: string
  $: string
}
export type PartialMessages<T extends Messages> = Partial<T> & {
  $: string
}
export function createMessages<T extends Messages>(messages: T): T {
  return messages
}
export const en = createMessages({
  $: 'English',
  welcome: 'Welcome!',
}
export const fr_ca = createMessages<PartialMessages<typeof en>>({
  $: 'Français (Canada)',
})

Expected behavior:

Should compile. Messages in PartialMessages are either complying or absent.

  • last tested working version: Typescript 3.7.5
  • first tested failing version: Typescript 3.8.2

Actual behavior:

Fail in:

export const fr_ca = createMessages<PartialMessages<typeof en>>(

Consider that all messages are present, but may be undefined.

Type 'PartialMessages<{ $: string; welcome: string; }>' does not satisfy the constraint 'Messages'.
  Property 'welcome' is incompatible with index signature.
    Type 'string | undefined' is not assignable to type 'Message'.
      Type 'undefined' is not assignable to type 'Message'.

Activity

  1. RyanCavanaugh commented on Mar 11, 2020

    @RyanCavanaugh
    Member
  2. MKRhere commented on Mar 11, 2020

    @MKRhere

    Partial<T> seems to make property values optional rather than the keys. Even though it seems like a semantic difference, a non-existent property cannot be accessed by messages.<prop_name>, while a property with a potentially non-existent value may be accessed to give type | undefined. Quick fix for now would be something like:

    export type Messages = Partial<{
        [key: string]: string,
    }> & {
        $: string
    }
  3. ahejlsberg commented on Mar 12, 2020

    @ahejlsberg
    Member

    This is working as intended and is an effect of #35143 (which was in the 3.8 release). Consider:

    // Compile with --strictNullChecks
    declare let t: { [key: string]: string, $: string };
    declare let s1: { welcome?: string } & { $: string };
    declare let s2: { welcome?: string, $: string };
    
    t = s1;  // Error, but wasn't before 3.8
    t = s2;  // Error

    Clearly the two assignments should be checked the same and they are now. Note that neither assignment is an error when --strictNullChecks is off.

  4. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    BugA bug in TypeScript
    on Mar 12, 2020
  5. sveyret commented on Mar 12, 2020

    @sveyret
    Author

    Thank you Anders Hejlsberg (@ahejlsberg). So, if I understand, the best way to write what I expect is how suggested by Muthu Kumar (@MKRhere)?
    I suggest to add this behavior (and the solution) in the « breaking change » list of 3.8…

  6. ahejlsberg commented on Mar 12, 2020

    @ahejlsberg
    Member

    Stéphane Veyret (@sveyret) I would probably change the declaration of Messages to

    export interface Messages {
      [key: string]: string | undefined;
      $: string;
    }
  7. sveyret commented on Mar 12, 2020

    @sveyret
    Author

    Yes, that's what I finally did. Thank you.
    Shall I close the issue?

  8. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions