How TypeScript is integrated in your editor

Rate this content
Bookmark

How can an editor ""automagically"" show type errors when you create a single TypeScript file without running ""npm install typescript"" or having a tsconfig.json file? Is a completion list build by your coding editor, TypeScript itself, or some other mysterious being? What is TSServer?
In this talk I will give you an overview of how the TypeScript server communicates with IDEs and other editors, delivering rich language features without ever running tsc.

This talk has been presented at TypeScript Congress 2023, check out the latest edition of this Tech Conference.

FAQ

The speaker is Maria Solano, a software engineer at Microsoft.

Maria Solano focuses on TypeScript editor tooling, LSP extensibility, and Visual Studio's JavaScript Project System.

The TypeScript inline variable refactor is used as an example to explain TypeScript integration in editors.

A code action is tied to a diagnostic and represents something you can do to potentially fix errors in your code.

Code actions are tied to diagnostics and fix errors, while refactorings are smart recommendations for writing cleaner code without necessarily indicating errors.

Editors recognize a trigger event for refactorings either through passive actions, like cursor placement, or explicit actions, like a configured keybinding.

TS-Server is the JavaScript and TypeScript language server that encapsulates the language compiler and other features, communicating with editors through a JSON protocol.

Editors communicate with TS-Server in two stages to first notify which refactors could be applied and then to compute the necessary file modifications only if the refactor is selected.

Different editors provide the same TypeScript experience by following the same TypeScript protocol defined by TS-Server.

TypeScript doesn't fully adopt LSP because transitioning would require a major rewrite of both the server and clients, and LSP remains language neutral, which may not support enhanced TypeScript-specific features effectively.

Maria Solano
Maria Solano
18 min
21 Sep, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk explores TypeScript integration and refactoring, code action handling, the TypeScript server and refactoring process, bug reporting, and the TypeScript protocol and LSP. The Talk discusses how TypeScript is integrated into editors, the role of code action providers, and the communication between the client and server. It also highlights the two-stage process of code actions and the importance of bug reporting. Additionally, it mentions the TypeScript protocol and how it allows for language-specific extensions. LSP is mentioned as a powerful extensibility solution used by various languages.

1. TypeScript Integration and Refactoring

Short description:

Hello, everyone! Today we'll explore how TypeScript is integrated into your editor, and who does what, and when. We'll use TypeScript inline variable refactor as an example. A code action is tied to a diagnostic, representing something that you can do to potentially fix errors. Refactoring provides smart recommendations for writing cleaner or nicer code. The editor recognizes trigger events, which can be passive or explicit. TypeScript shows refactorings based on trigger kind, and you can explicitly request a refactor for variables.

Hello, everyone, and thank you for joining me today. I'm Maria Solano, and I'm a software engineer at Microsoft. Quickly introducing myself, I focus on TypeScript editor tooling, but sometimes I also dive into LSP extensibility, Visual Studio's JavaScript Project System, or I just stare at the title TypeScript checker for a couple of hours, trying to absorb its wisdom, as I'm sure all of you have done.

Today we'll explore how TypeScript is integrated into your editor, and who does what, and when. For this, we'll use TypeScript inline variable refactor as an example, which is my favorite, mainly because I implemented it. Say that you have this print greeting function, and your cursor is in the first line of its body. In an editor like VS Code, you'll see a lightbulb. Where is that lightbulb coming from? Spoiler alert, if you click on it, a list appears. Who constructs a list and how are items inserted into that list? Not only VS Code has this functionality, Visual Studio, NeoVim, Emacs, Zed, and other obscure editors also have a TypeScript lightbulb experience that doesn't differ much from the one in VS Code. How is that done?

As a side note, during this talk I'll be using code action and refactoring as synonyms, despite that they're not exactly the same. Although the difference is a technicality, I'll explain it anyway just so that you can understand when your next code editor trivia game. A code action is tied to a diagnostic, and hence represents something that you can do to potentially fix those errors. Refactoring, on the other hand, aren't corrections, they're just smart recommendations for writing cleaner or nicer code, but it doesn't mean that there's something wrong with what you wrote.

Going back to PrintGreeting, the first thing that happens is that the editor will recognize a trigger event. This can be a passive action, such as your cursor being placed in a position where a refactoring could be applied. The trigger could also be explicit, such as one that you configure with a keybinding. Note that results could differ based on the trigger kind. Having a constant lightbulb popping up everywhere on your screen could be annoying, and so TypeScript might decide to show you certain refactorings only if you really want to see them. In this example, TypeScript will show a lightbulb besides the identifier of a variable that could be inlined, but you'll need to explicitly request the refactor in references of such variables.

2. Code Action Handling and Providers

Short description:

The editor needs someone who can understand the code to determine which refactorings to display. It records the position and content type, which may not directly correspond to the file extension. Extensions can handle code action requests using different mechanisms, such as registering a callback. Multiple code action providers can enhance the fixes provided by the language server.

Now say that we are no-beam gurus and use the mouse to click on the lightbulb. The editor doesn't actually know which refactorings to display here. It needs someone who can understand the code.

To communicate with that someone, the editor will record the position as well as the content type. Note that the content type isn't exactly a 1-to-1 mapping with the type coming from the file extension. A TypeScript snippet inside a script block of an HTML file is also assigned to the TypeScript content type. This is how you can still get code action, completions and the correct syntax highlighting in embedded code.

An extension announces that it can handle the code action request. There are different mechanisms to do so. In VS Code, you would register a code action provider for the TypeScript content type. In Visual Studio, you could use Roslyn APIs or the language service protocol. Most of the time, this basically involves registering a callback and telling the editor when to invoke it. Note that this is unlimited to language servers. Extensions like ESLint can also hook up into the code action list to enhance the fixes provided by the language server. This also means that there can be multiple code action providers coming from different extensions. The editor will then combine all of the results into a single list.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced Conference 2022React Advanced Conference 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
This Talk discusses modern web debugging and the latest updates in Chrome DevTools. It highlights new features that help pinpoint issues quicker, improved file visibility and source mapping, and ignoring and configuring files. The Breakpoints panel in DevTools has been redesigned for easier access and management. The Talk also covers the challenges of debugging with source maps and the efforts to standardize the source map format. Lastly, it provides tips for improving productivity with DevTools and emphasizes the importance of reporting bugs and using source maps for debugging production code.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.

Workshops on related topic

React Hooks Tips Only the Pros Know
React Summit Remote Edition 2021React Summit Remote Edition 2021
177 min
React Hooks Tips Only the Pros Know
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.
React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.