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.

Can type definitions be more automatic for beginners?

See original GitHub issue

I’ve seen a lot of people ask how they can get type definitions for Cypress in JavaScript. As it is right now, this in not automatic. We spent a lot of time getting type definitions to “just work” for Cypress commands and various aspects of the Cypress API, but many people aren’t getting that benefit. I’ve seen some people have success making a tsconfig.json file to force the TypeScript language service to work with Cypress type definitions. This definitely works better for VSCode. I’ve seen people give up trying to get it to work with WebStorm before just switching to VSCode.

I use the tsconfig.json approach since my projects are written in TypeScript and that config file already exists. I don’t have much experience with VSCode or other editors writing JavaScript and how types are pulled in. For most libraries that have exports, type definitions should be pulled in with a .d.ts file next to the source sharing the same base name. Cypress defines globals so type definitions files need to be manually included. Maybe a solutions is for when Cypress lays down support files (config, folders, examples, etc), also lay down a tsconfig.json file? Anyone with experience with WebStorm or editors? Would that solution work?

  • Operating System: N/A
  • Cypress Version: 1.4.1
  • Browser Version: N/A

Is this a Feature or Bug?

Feature

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

7reactions
bahmutovcommented, Jan 29, 2018

Actually I found a perfect way to use our types @NicholasBoll

Just add a comment to the JS spec file

/// <reference types="Cypress" />

Nothing else is necessary (neither typescript compiler, not jsconfig.json, nor tsconfig.json).

Example: spec file without anything. No IntelliSense

spec

Spec file with reference comment: full IntelliSense in VSCode

spec-with-reference
3reactions
hafcommented, Sep 23, 2018

I’m also trying to get this set up; I’ve followed the guide as near as I could (but I did install cypress with yarn). I can start cypress and I can write tests, but I can’t get typescript code competition to work, despite follow the docs and disabling any plugins I can imagine disrupt code completition. Here’s what my workspace looks like:

screen shot 2018-09-23 at 14 06 57 screen shot 2018-09-23 at 14 07 39

I’ve tried with tsconfig.json in both project root (one below) and in cypress/ folder, but to no avail. I’ve also npm install -g typescript to get the compiler, so that these command show their respective outputs https://code.visualstudio.com/docs/languages/typescript. Also, like you can see, I’ve added the triple-slash directive on top of the file.


EDIT: I had a look at the Developer Console in VSCode and it had lots of typescript errors… I then added this to tsconfig.json:

{
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
   ...
}

and restarted VSCode in the folder with tsconfig.json in it. Now it started working.


EDIT 2: that was probably because I selected “Typescript” in the bottom language selector drop-up. However, adding "checkJs": true, to tsconfig.js and restarting seemingly now has turned on TS completion for all JS files in the cypress project.

image
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Typings: The Complete Guide: @types Compiler ...
A library does not ship with type definitions, but they can be installed separately; A library ships with its own type definitions built-in....
Read more >
TypeScript: Adding Custom Type Definitions for Existing ...
Whether or not a library has type definitions is a big factor in deciding whether I'll use it. You can find the type...
Read more >
Documentation - Type Declarations - TypeScript
TypeScript automatically finds type definitions under node_modules/@types , so there's no other step needed to get these types available in your program.
Read more >
Using auto whenever possible | Modern C++ Programming ...
The new C++ standards have made it possible to use auto as a placeholder for types in various contexts and let the compiler...
Read more >
Functions - Definition & Usage - AutoHotkey
Consequently, execution can never fall into a function from above, nor does the presence of one or more functions at the very top...
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