Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 98538

Different Typescript inference management between vscode and monaco-editor

$
0
0

I just made a complex function that take 3 arguments : name, types and method. This function stores a method in a store. It infer the return type of the third agument from the second one.

addMethod.d.ts

interface SimplifiedTypeMap {
  string: string;
  number: number;
  boolean: boolean;
}

type GlobalMethodAdd = <T extends keyof SimplifiedTypeMap>(
  name: string,
  types: T[],
  method: () => SimplifiedTypeMap[T]
) => void;

interface MethodStore {
  [name: string]: {
    types: (keyof SimplifiedTypeMap)[];
    method: () => SimplifiedTypeMap[keyof SimplifiedTypeMap];
  };
}

Thanks to typescript engine, the return type of the last argument (method) is inferred from the items in the second argument (types) and int forces the user of the function to write a method with a specific return type

addMethod.ts

import { random } from "lodash-es";

export const methodStorage: MethodStore = {};

const addMethod: GlobalMethodAdd = (name, types, method) => {
  methodStorage[name] = { types, method };
};

addMethod("test", ["string", "number"], () =>
  random(1, true) > 0.5 ? "abcd" : 1234
);

When i'm using the addMethod function on Visual-Studio Code or Codesandbox, the return type of the third argument is well known but not on monaco-editor :

visual studio codeenter image description here

codesandbox editorenter image description here

(FAILING) monaco editorenter image description here

Here is my example in codesandbox

=========================================================================

EDIT I found out that it's the usage of noLib compiler option that makes that happen.

reactMonaco.languages.typescript.typescriptDefaults.setCompilerOptions({
  target: reactMonaco.languages.typescript.ScriptTarget.ES5,
  noLib: true,
  allowNonTsExtensions: true,
  checkJs: true,
});

Is there a way to avoid es5 libs autocompletion while keeping inference working properly?


Viewing all articles
Browse latest Browse all 98538

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>