microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.99k stars 12.48k forks source link

Wrong code suggestion when using "Mapped Types" as function parameter #55545

Closed lNaymonl closed 1 year ago

lNaymonl commented 1 year ago

Type: Bug

Extension version: 5.3.20230827

Version: 1.81.1 (user setup) Commit: 6c3e3dba23e8fadc360aed75ce363ba185c49794 Date: 2023-08-09T22:22:42.175Z Electron: 22.3.18 ElectronBuildId: 22689846 Chromium: 108.0.5359.215 Node.js: 16.17.1 V8: 10.8.168.25-electron.0 OS: Windows_NT x64 10.0.19045

Problem: The Codesuggestion shows "1", "2", "3", "4" event though a is "valA". When hovering it also shows that b has type "1" | "2". And when i select "3" or "4" it gives me the following error: Argument of type '"3"' is not assignable to parameter of type '"1" | "2"'.

Screenshot (7)

Expected: The Codesuggestion should only display"1" and "2".

Source Code:

enum myEnum {
  valA = "valA",
  valB = "valB",
}

interface myEnumParamMapping {
  ["valA"]: "1" | "2";
  ["valB"]: "3" | "4";
}

function myFunction<K extends keyof typeof myEnum>(a: K, b: myEnumParamMapping[K]) {
  console.log({ a, b });
}

myFunction("valA", "2");
13OnTheCode commented 1 year ago

微信截图_20230829014357

@lNaymonl My TypeScript version is 5.2.2, and when I tried to reproduce your issue, everything seems to be working fine, unless I missed something.

andrewbranch commented 1 year ago

I can reproduce this. @lNaymonl @13OnTheCode could you both post your tsconfig.json settings please?

13OnTheCode commented 1 year ago

@andrewbranch

"compilerOptions": {
    "strict": true,
    "noEmit": true,
    "allowJs": true,
    "skipLibCheck": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "strictNullChecks": true,
    "resolveJsonModule": true,
    "useDefineForClassFields": true,
    "noUncheckedIndexedAccess": true,
    "allowImportingTsExtensions": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true
}
andrewbranch commented 1 year ago

Still repros for me with those settings 👀

andrewbranch commented 1 year ago

@Andarist does this look familiar to you?

Andarist commented 1 year ago

Yes, Im already investigating this and gonna likely put up a PR for this soon-ish

andrewbranch commented 1 year ago

Could have sworn either you or I have fixed this about 20 times already in the past

lNaymonl commented 1 year ago

I can reproduce this. @lNaymonl @13OnTheCode could you both post your tsconfig.json settings please?

{
"compileOnSave": false,
"compilerOptions": {
"downlevelIteration": true,
"module": "es2020",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2018",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"baseUrl": ".",
"paths": {
"globalize": [
"../node_modules/globalize/dist/globalize"
],
"globalize/*": [
"../node_modules/globalize/dist/globalize/*"
],
"cldr": [
"../node_modules/cldrjs/dist/cldr"
],
"cldr/*": [
"../node_modules/cldrjs/dist/cldr/*"
]
}
}
}
lNaymonl commented 1 year ago

If i try it in VS, it works like a charm. I also tried it without the vscode extension, which worked as well.

Andarist commented 1 year ago

I prepared a fix for this here: https://github.com/microsoft/TypeScript/pull/55552