Skip to content

Typesafety violation #62043

Open
Open
@jacqueslenormand

Description

@jacqueslenormand

🔎 Search Terms

The following typechecks in version 5.8.3 (but shouldn't):

type Endpoints = {
  "/foo/bar": {
    output: {bar: boolean}
  },
  "/foo/bar2": {
    output: {bar2: boolean}
  },
}

function act<Path extends keyof Endpoints>(path: Path): Endpoints[Path]["output"] {
  if(path === "/foo/bar")  {
    return {bar2: true}
  }
  return {bar: false}  
}

const u : {bar: boolean} /* {bar:boolean} is incorrect */ = act("/foo/bar")
console.log(u) // {bar2: true}

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about version 5.8.3
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAogdgEzAewJZ2AZygXigbwCgooAiAegDNllyAjAQwCdSAuA4kqZAV2DD7t8jJuzo0ANhAZwAvp1kAaThWq0RAJjYcu3PgOBDNYydLkLl8wpR5wAxsFTI4UShGB2AFgB4ACg2BPKAgAD2AIRGwAawgQZEpYRBR0LAA+AAowAM92f0CASnZ4JDQMTABtPM8AXXLSXn4+UmqdKFRKTOzcHDxVGnpmUnySIl0mdx4mF2FmDXZgJh4IeRIVqHHgSemRdkoGCUxlkkIrO2dMYCgeKCNmE2QpGVlcV3cvdL71QfzCM7hMB4QAB0EmQAHN0jx8kA

💻 Code

type Endpoints = {
  "/foo/bar": {
    output: {bar: boolean}
  },
  "/foo/bar2": {
    output: {bar2: boolean}
  },
}

function act<Path extends keyof Endpoints>(path: Path): Endpoints[Path]["output"] {
  if(path === "/foo/bar")  {
    return {bar2: true}
  }
  return {bar: false}  
}

const u : {bar: boolean} /* {bar:boolean} is incorrect */ = act("/foo/bar")
console.log(u) // {bar2: true}

🙁 Actual behavior

It typechecks, but shouldn't. act("/foo/bar") has the wrong type.

🙂 Expected behavior

I would have expected a type error

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions