question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type Checking in JS File allows invalid JavaScript

See original GitHub issue

monaco-editor version: Latest Browser: Chrome OS: Windows Playground code that reproduces the issue: Extending Language Services > Configuring JavaScript Defaults

(Set noSemanticValidation to false on line 8)

// Add additonal d.ts files to the JavaScript language service and change.
// Also change the default compilation options.
// The sample below shows how a class Facts is declared and introduced
// to the system and how the compiler is told to use ES6 (target=2).

// validation settings
monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
	noSemanticValidation: false,
	noSyntaxValidation: false
});

// compiler options
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
	target: monaco.languages.typescript.ScriptTarget.ES6,
	allowNonTsExtensions: true
});

// extra libraries
var libSource = [
	'declare class Facts {',
	'    /**',
	'     * Returns the next fact',
	'     */',
	'    static next():string',
	'}',
].join('\n');
var libUri = 'ts:filename/facts.d.ts';
monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
// When resolving definitions and references, the editor will try to use created models.
// Creating a model for the library allows "peek definition/references" commands to work with the library.
monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));

var jsCode = [
	'"use strict";',
	'',
	'class Chuck {',
	'    greet() {',
	'        return Facts.next();',
	'    }',
	'}'
].join('\n');

monaco.editor.create(document.getElementById('container'), {
	value: jsCode,
	language: 'javascript'
});

In this editor, the language is set to javascript and semantic validation is turned on for type checking. (Equivalent to // @ts-check in VSCode).

Since the compiler is TypeScript, it allows any valid TypeScript, which can be invalid JavaScript.

Take the following code block for example:

let x = 10;
x.nonExistantMethod();

let y: any = 20;

type MyType = string;

Expected Behaviour (from VSCode)

let x = 10;
x.nonExistantMethod(); // Error: Property 'nonExistantMethod' does not exist on type 'number'.

let y: any = 20; // Error: Type annotations can only be used in TypeScript files.

type MyType = string; // Error: Type aliases can only be used in TypeScript files.

Actual Behaviour (in playground)

let x = 10;
x.nonExistantMethod(); // Error: Property 'nonExistantMethod' does not exist on type 'number'.

let y: any = 20; // No Error

type MyType = string; // No Error

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
giacomorebonatocommented, Feb 11, 2022

I am using the following configuration:

  monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
    noSemanticValidation: false,
    noSyntaxValidation: false
  });
  monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
    target: monaco.languages.typescript.ScriptTarget.ES6,
    allowJs: true,
    checkJs: true,
    allowNonTsExtensions: true
  });
  monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource);

I then use JSDoc to reference the type and it works for intellisense, but if I break the type requirement then I don’t see the red mark that I expect. Is it supposed to work now?

Please let me know if I should provide a playground with an example.

0reactions
alexdimacommented, Jun 10, 2021

@orta What would you recommend? Here are the JS defaults.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Type Checking JavaScript Files - TypeScript
In a .js file, the compiler infers properties from property assignments inside the class body. The type of a property is the type...
Read more >
Allow minimal type checking of JavaScript files #28448 - GitHub
I'm in the process of introducing TypeScript support into an enormous, 6-year old web application with a lot of JavaScript files.
Read more >
Working with JavaScript in Visual Studio Code
The easiest way to enable type checking in a JavaScript file is by adding // @ts-check to the top of a file. Using...
Read more >
Type Checking JavaScript Files
Here are some notable differences on how checking works in .js files ... with null or undefined will have type any, even if...
Read more >
Typescript allows to save wrong types. why? - Stack Overflow
TypeScript doesn't check any types at runtime. If you use X as Y you basically pinky-swear that the data X is of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found