Content-Length: 268069 | pFad | http://github.com/microsoft/TypeScript/issues/61754

08 Spreading constrained generic parameters allows for invalid/unsafe return types Β· Issue #61754 Β· microsoft/TypeScript Β· GitHub
Skip to content

Spreading constrained generic parameters allows for invalid/unsafe return typesΒ #61754

Open
@WadePeterson

Description

@WadePeterson

πŸ”Ž Search Terms

spread generic
spread generic unsafe
spread constrained generic invalid

πŸ•— Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about "generics" and "spread"

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgILIN4Chm+XALmQGcwpQBzHPAIyJAFcBbG6LAXyyxgZATGAB7EMkikAjAAoAJnDCE0ASiLpseZFAhgGUEWvV4AdMdnyANNQPI6yAOQxgANwi3kAejfIEgqJv4AbAE9kaCgfYiIAFUCABxRbUnIQCldgYmQQQTB8YmJgChA4Gn8UMEFRWPjGFmhbQ0tkTk5uXn4hETEwACYAHkiQgA9IEGl01AA+GTkFSOU0TAbNbV0Fq1xjQ1M4CzXrInsnF3dPaUEIdMzs0J8zawZs4gALQQZ-aUMNLR0QdNBHOH8wGkyEENAAVhB+A0mlggA

πŸ’» Code

interface A {
    a: string
    b: number
}

function test1(data: A): A {
    return {
        ...data,
        b: 'five' // correctly errors: Type 'string' is not assignable to type 'number'.
    }
}

function test2<T extends A>(data: T): A {
    return {
        ...data,
        b: 'five' // does not error, but should. returns invalid object
    }
}

πŸ™ Actual behavior

test2 function compiles without error, but when called at runtime, will always return an invalid object, with property b of type string instead of number

πŸ™‚ Expected behavior

test2 function has a compiler error preventing spreading of the b property with an invalid value.

Additional information about the issue

I found a similar issue, #60623

However, this issue has a more complex case, with an example function returning a generic. In my example, test2 declares a static return type, and unambiguously returns an invalid value that does not satisfy that type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: http://github.com/microsoft/TypeScript/issues/61754

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy