<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Psychology Experiments on Rahat Zaman</title><link>https://rahatzaman.me/tags/psychology-experiments/</link><description>Recent content in Psychology Experiments on Rahat Zaman</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>rahatzamancse@gmail.com (Rahat Zaman)</managingEditor><webMaster>rahatzamancse@gmail.com (Rahat Zaman)</webMaster><lastBuildDate>Wed, 23 Oct 2024 10:21:45 -0600</lastBuildDate><sy:updatePeriod>monthly</sy:updatePeriod><sy:updateFrequency>monthly</sy:updateFrequency><atom:link href="https://rahatzaman.me/tags/psychology-experiments/index.xml" rel="self" type="application/rss+xml"/><item><title>JsPsych Experiment with Vite and TypeScript</title><link>https://rahatzaman.me/posts/jspsych-vite-typescript/</link><pubDate>Wed, 23 Oct 2024 10:21:45 -0600</pubDate><author>rahatzamancse@gmail.com (Rahat Zaman)</author><atom:modified>Wed, 17 Sep 2025 14:48:39 -0600</atom:modified><guid>https://rahatzaman.me/posts/jspsych-vite-typescript/</guid><description>&lt;p&gt;In this guide, we&amp;rsquo;ll walk you through setting up a simple psychology experiment using JsPsych within a Vite and TypeScript project. We&amp;rsquo;ll assume you have little to no prior experience with JsPsych, Vite, or TypeScript. This step-by-step guide will explain the basics and get you up and running quickly.&lt;/p&gt;
&lt;h3 id="what-is-jspsych"&gt;What is JsPsych?&lt;/h3&gt;
&lt;p&gt;JsPsych is a library for creating behavioral experiments that run in a web browser. It is often used in psychology and cognitive science research to create experiments that participants can complete online.&lt;/p&gt;</description><content:encoded>&lt;![CDATA[&lt;p&gt;In this guide, we&amp;rsquo;ll walk you through setting up a simple psychology experiment using JsPsych within a Vite and TypeScript project. We&amp;rsquo;ll assume you have little to no prior experience with JsPsych, Vite, or TypeScript. This step-by-step guide will explain the basics and get you up and running quickly.&lt;/p&gt;
&lt;h3 id="what-is-jspsych"&gt;What is JsPsych?&lt;/h3&gt;
&lt;p&gt;JsPsych is a library for creating behavioral experiments that run in a web browser. It is often used in psychology and cognitive science research to create experiments that participants can complete online.&lt;/p&gt;
&lt;h3 id="why-use-vite-and-typescript-with-jspsych"&gt;Why Use Vite and TypeScript with JsPsych?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vite&lt;/strong&gt; helps you develop web applications quickly, refreshing the page whenever you make changes, which speeds up development.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; ensures that your code is easier to read and less prone to errors by adding extra checks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JsPsych&lt;/strong&gt; provides the structure and tools needed to create an experiment, such as displaying stimuli and recording participant responses.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, let’s set up everything step by step.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="step-1-set-up-a-vite--typescript-project"&gt;Step 1: Set Up a Vite + TypeScript Project&lt;/h2&gt;
&lt;p&gt;If you haven&amp;rsquo;t already set up a Vite + TypeScript project, follow these instructions:&lt;/p&gt;
&lt;h3 id="11-open-your-terminal"&gt;1.1 Open Your Terminal&lt;/h3&gt;
&lt;p&gt;Start by opening your &lt;strong&gt;Terminal&lt;/strong&gt; (or &lt;strong&gt;Command Prompt&lt;/strong&gt; on Windows).&lt;/p&gt;
&lt;h3 id="12-create-a-new-vite-project"&gt;1.2 Create a New Vite Project&lt;/h3&gt;
&lt;p&gt;In the terminal, type the following command and press Enter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm create vite@latest
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Answer the questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Project name&lt;/strong&gt;: Enter a name (e.g., &lt;code&gt;jspsych-experiment&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Framework&lt;/strong&gt;: Choose &lt;code&gt;vanilla&lt;/code&gt; (basic JavaScript).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variant&lt;/strong&gt;: Choose &lt;code&gt;TypeScript&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Once the project is created, move into your new project folder:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; jspsych-experiment
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="13-install-dependencies"&gt;1.3 Install Dependencies&lt;/h3&gt;
&lt;p&gt;Run the following command to install all the required files:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm install
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;To know more about what these steps are doing, check out the &lt;a href="https://rahatzaman.me/posts/intro-to-vite-typescript/"&gt;Introduction to Vite + Typescript&lt;/a&gt; tutorial.&lt;/p&gt;
&lt;p&gt;Now, you’ve set up your Vite + TypeScript project!&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="step-2-install-jspsych"&gt;Step 2: Install JsPsych&lt;/h2&gt;
&lt;p&gt;JsPsych is not included by default in your Vite project, so you need to install it manually.&lt;/p&gt;
&lt;p&gt;In the terminal, run the following command to add JsPsych to your project:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm install jspsych &lt;span class="c1"&gt;# to install the core library&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm install @jspsych/plugin-button-response &lt;span class="c1"&gt;# to install the button-response plugin&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;This will download JsPsych and add it to your project’s dependencies.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="step-3-modify-your-project-files"&gt;Step 3: Modify Your Project Files&lt;/h2&gt;
&lt;p&gt;Next, we&amp;rsquo;ll edit the project files to display a simple JsPsych experiment.&lt;/p&gt;
&lt;h3 id="31-set-up-your-html-file"&gt;3.1 Set Up Your HTML File&lt;/h3&gt;
&lt;p&gt;First, let&amp;rsquo;s modify the &lt;code&gt;index.html&lt;/code&gt; file, which is the main web page for your experiment.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;index.html&lt;/code&gt; in your code editor (like VS Code).&lt;/li&gt;
&lt;li&gt;Modify it to include a placeholder where the JsPsych experiment will be displayed:
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;html&lt;/span&gt; &lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;en&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;head&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;meta&lt;/span&gt; &lt;span class="na"&gt;charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;UTF-8&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;meta&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;viewport&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;width=device-width, initial-scale=1.0&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;JsPsych Experiment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;head&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome to My JsPsych Experiment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;jspsych-experiment&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;module&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;/src/main.ts&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="32-create-a-simple-jspsych-experiment"&gt;3.2 Create a Simple JsPsych Experiment&lt;/h3&gt;
&lt;p&gt;Now, let’s write the code for a basic JsPsych experiment in &lt;code&gt;main.ts&lt;/code&gt;. This code will create a simple trial where the participant sees a message and responds by pressing a key.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;src/main.ts&lt;/code&gt; in your editor.&lt;/li&gt;
&lt;li&gt;Add the following code:
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;initJsPsych&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="kr"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;jspsych&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;jsPsychButtonResponse&lt;/span&gt; &lt;span class="kr"&gt;from&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;@jspsych/plugin-button-response&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jsPsych&lt;/span&gt;: &lt;span class="kt"&gt;JsPsych&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jsPsychInit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Create a simple timeline with one trial
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;&lt;/span&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;timeline&lt;/span&gt;: &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;trial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kr"&gt;type&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;jsPsychButtonResponse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;stimulus&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;lt;p&amp;gt;Press any key to begin the experiment.&amp;lt;/p&amp;gt;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;timeline&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trial&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Initialize the JsPsych experiment
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;&lt;/span&gt;&lt;span class="nx"&gt;jsPsych&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timeline&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="breakdown-of-the-code"&gt;Breakdown of the Code:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;import { initJsPsych } from 'jspsych';&lt;/code&gt;: This imports the JsPsych library into your project.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const jsPsych: JsPsych = initJsPsych();&lt;/code&gt;: This initializes JsPsych and assigns it to the &lt;code&gt;jsPsych&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const timeline: any[] = [];&lt;/code&gt;: A timeline in JsPsych is a list of trials that define your experiment. We create an empty timeline and add our trials to it.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const trial = {...}&lt;/code&gt;: This is a basic trial that presents a message and waits for the participant to press any key.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;jsPsych.run(timeline);&lt;/code&gt;: This starts the experiment using the timeline we created.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2 id="step-4-run-the-experiment"&gt;Step 4: Run the Experiment&lt;/h2&gt;
&lt;p&gt;With everything set up, you&amp;rsquo;re ready to run the experiment and see it in action!&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Open the terminal (if it’s not already open).&lt;/li&gt;
&lt;li&gt;Run the development server with this command:
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm run dev
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Vite will start a local development server and give you an address like &lt;code&gt;http://localhost:3000/&lt;/code&gt;. Open this address in your web browser.&lt;/p&gt;
&lt;p&gt;You should see your experiment with the message &amp;ldquo;Press any key to begin the experiment.&amp;rdquo; When you press a key, the trial will complete.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You have successfully set up a simple JsPsych experiment using Vite and TypeScript! Here’s what we did in this tutorial:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Created a Vite + TypeScript project.&lt;/li&gt;
&lt;li&gt;Installed JsPsych to run psychology experiments.&lt;/li&gt;
&lt;li&gt;Wrote a simple experiment where a participant presses a key.&lt;/li&gt;
&lt;li&gt;Ran the experiment in a browser.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="recap"&gt;Recap:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vite&lt;/strong&gt; makes your development faster by reloading changes automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; helps catch mistakes in your code early.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JsPsych&lt;/strong&gt; allows you to easily create browser-based behavioral experiments.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now you can continue to build more complex experiments by adding more trials and customizing how participants interact with the experiment. Happy experimenting!&lt;/p&gt;
]]></content:encoded><dc:creator>Rahat Zaman</dc:creator><media:content url="https://rahatzaman.me//images/thumbs/jspsych_logo.png" medium="image"><media:title type="html">featured image</media:title></media:content><category>JsPsych</category><category>Vite</category><category>TypeScript</category><category>Psychology Experiments</category></item><item><title>Introduction to Vite Typescript</title><link>https://rahatzaman.me/posts/intro-to-vite-typescript/</link><pubDate>Wed, 23 Oct 2024 09:58:43 -0600</pubDate><author>rahatzamancse@gmail.com (Rahat Zaman)</author><atom:modified>Wed, 17 Sep 2025 14:48:39 -0600</atom:modified><guid>https://rahatzaman.me/posts/intro-to-vite-typescript/</guid><description>&lt;p&gt;This guide will walk you through setting up a project using Vite and TypeScript from scratch. Don&amp;rsquo;t worry if you&amp;rsquo;re unfamiliar with programming or technical terms. We&amp;rsquo;ll explain everything in simple terms.&lt;/p&gt;
&lt;h2 id="what-is-vite"&gt;What Is Vite?&lt;/h2&gt;
&lt;p&gt;Before diving into the steps, let&amp;rsquo;s first understand what Vite is and why it&amp;rsquo;s useful.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vite&lt;/strong&gt; (pronounced like &amp;ldquo;veet&amp;rdquo;) is a tool that helps you develop websites. When building a website, you usually need to set up many things—like how to handle multiple files and make your website load quickly. Vite takes care of all that for you, so you can focus on writing your website&amp;rsquo;s code.&lt;/li&gt;
&lt;li&gt;It makes development faster, especially when your project gets larger.&lt;/li&gt;
&lt;li&gt;Instead of loading everything at once (which can make your website slow), Vite loads only what&amp;rsquo;s needed when you start developing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Why use Vite? Let&amp;rsquo;s look at an example:&lt;/p&gt;</description><content:encoded>&lt;![CDATA[&lt;p&gt;This guide will walk you through setting up a project using Vite and TypeScript from scratch. Don&amp;rsquo;t worry if you&amp;rsquo;re unfamiliar with programming or technical terms. We&amp;rsquo;ll explain everything in simple terms.&lt;/p&gt;
&lt;h2 id="what-is-vite"&gt;What Is Vite?&lt;/h2&gt;
&lt;p&gt;Before diving into the steps, let&amp;rsquo;s first understand what Vite is and why it&amp;rsquo;s useful.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vite&lt;/strong&gt; (pronounced like &amp;ldquo;veet&amp;rdquo;) is a tool that helps you develop websites. When building a website, you usually need to set up many things—like how to handle multiple files and make your website load quickly. Vite takes care of all that for you, so you can focus on writing your website&amp;rsquo;s code.&lt;/li&gt;
&lt;li&gt;It makes development faster, especially when your project gets larger.&lt;/li&gt;
&lt;li&gt;Instead of loading everything at once (which can make your website slow), Vite loads only what&amp;rsquo;s needed when you start developing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Why use Vite? Let&amp;rsquo;s look at an example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Imagine you&amp;rsquo;re building a website using plain HTML and JavaScript (the language used to make websites interactive). As your website grows, it gets slower because you have to load all the files at once. Vite helps avoid this by loading just the files you&amp;rsquo;re working on, making it much faster to work with.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="why-is-vite-better-than-plain-html-and-javascript"&gt;Why is Vite Better than Plain HTML and JavaScript?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Faster development&lt;/strong&gt;: Vite refreshes only the part of the page you&amp;rsquo;re working on, while with plain HTML/JavaScript, you&amp;rsquo;d have to reload the entire page.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Organized code&lt;/strong&gt;: You can split your code into smaller parts, which Vite helps combine neatly.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Support for modern technologies&lt;/strong&gt;: It makes it easier to use new web development features like TypeScript.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="what-is-typescript"&gt;What is TypeScript?&lt;/h2&gt;
&lt;p&gt;Now, let&amp;rsquo;s talk about TypeScript:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; is a programming language that builds on JavaScript. It adds extra features, such as &amp;ldquo;types,&amp;rdquo; which help you avoid common mistakes when writing code.&lt;/li&gt;
&lt;li&gt;It helps make your code more predictable, reducing the chances of bugs.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You don’t need to fully understand TypeScript yet to follow this guide, but think of it as a &amp;ldquo;safer&amp;rdquo; version of JavaScript that helps you write cleaner, less error-prone code.&lt;/p&gt;
&lt;h3 id="why-use-typescript"&gt;Why Use TypeScript?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Catch errors early&lt;/strong&gt;: TypeScript checks your code for potential problems before it runs, so you can fix them before they break your website.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Better collaboration&lt;/strong&gt;: If you&amp;rsquo;re working with other developers, TypeScript makes it easier for everyone to understand how the code works.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Now that we know why Vite and TypeScript are useful, let&amp;rsquo;s get started with setting them up.&lt;/p&gt;
&lt;h2 id="step-1-setting-up-your-development-environment"&gt;Step 1: Setting Up Your Development Environment&lt;/h2&gt;
&lt;p&gt;Before we can use Vite and TypeScript, you&amp;rsquo;ll need to install a few basic tools:&lt;/p&gt;
&lt;h3 id="11-install-nodejs"&gt;1.1 Install Node.js&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Node.js&lt;/strong&gt; is a tool that lets you run JavaScript (and TypeScript) outside of a web browser. Vite uses Node.js to manage your website&amp;rsquo;s files during development.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en"&gt;Download Node.js&lt;/a&gt; from its official website.
&lt;ul&gt;
&lt;li&gt;Choose the &lt;strong&gt;LTS&lt;/strong&gt; version (it stands for Long Term Support, meaning it is more stable).&lt;/li&gt;
&lt;li&gt;Once downloaded, install it by following the on-screen instructions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="12-install-a-code-editor"&gt;1.2 Install a Code Editor&lt;/h3&gt;
&lt;p&gt;You&amp;rsquo;ll need a place to write your code. The most popular tool for this is &lt;strong&gt;Visual Studio Code (VS Code)&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/"&gt;Download Visual Studio Code&lt;/a&gt; from its official site and install it.&lt;/li&gt;
&lt;li&gt;This editor comes with useful features like error checking and highlighting code, which makes it beginner-friendly.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2 id="step-2-starting-a-vite--typescript-project"&gt;Step 2: Starting a Vite + TypeScript Project&lt;/h2&gt;
&lt;p&gt;With Node.js installed, you can create your Vite + TypeScript project.&lt;/p&gt;
&lt;h3 id="21-open-your-terminal"&gt;2.1 Open Your Terminal&lt;/h3&gt;
&lt;p&gt;On your computer, open the &lt;strong&gt;Terminal&lt;/strong&gt; or &lt;strong&gt;Command Prompt&lt;/strong&gt;. If you&amp;rsquo;re using Windows, search for &amp;ldquo;Command Prompt&amp;rdquo; in the Start menu.&lt;/p&gt;
&lt;h3 id="22-create-a-new-vite-project"&gt;2.2 Create a New Vite Project&lt;/h3&gt;
&lt;p&gt;Now, let&amp;rsquo;s create your first Vite project:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;In the terminal, type the following command and press Enter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm create vite@latest
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;This command tells Node.js to create a new project using Vite.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;You&amp;rsquo;ll be asked a few questions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Project name&lt;/strong&gt;: Enter a name for your project (e.g., &lt;code&gt;my-vite-project&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Framework&lt;/strong&gt;: Choose &lt;code&gt;vanilla&lt;/code&gt; (this means basic JavaScript).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variant&lt;/strong&gt;: Choose &lt;code&gt;TypeScript&lt;/code&gt; (this sets up TypeScript automatically for you).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once you&amp;rsquo;ve answered the questions, the terminal will set up your project. Next, move into the project folder by typing:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; my-vite-project
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="23-install-dependencies"&gt;2.3 Install Dependencies&lt;/h3&gt;
&lt;p&gt;Next, you&amp;rsquo;ll need to install some files that Vite needs to run your project. These are called &lt;strong&gt;dependencies&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Run this command in your terminal:
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm install
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This will download all the necessary files.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="step-3-running-your-project"&gt;Step 3: Running Your Project&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;re now ready to start your Vite project and see it in action!&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;In the terminal, type the following command:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;npm run dev
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Vite will start a local server, and it will tell you an address to visit in your web browser, something like:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;Local: http://localhost:3000/
&lt;/code&gt;&lt;/pre&gt;&lt;ul&gt;
&lt;li&gt;Open your browser and go to this address. You should see a page that says &lt;strong&gt;Vite + TypeScript&lt;/strong&gt;. Congratulations! You now have a basic Vite project running with TypeScript.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;hr /&gt;
&lt;h2 id="step-4-understanding-your-projects-structure"&gt;Step 4: Understanding Your Project&amp;rsquo;s Structure&lt;/h2&gt;
&lt;p&gt;Let&amp;rsquo;s break down what Vite has created for you:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;index.html&lt;/strong&gt;: This is the main page of your website. It tells the browser what content to display.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;main.ts&lt;/strong&gt;: This is the main TypeScript file. It is where you will write your code to make the page interactive.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;style.css&lt;/strong&gt;: This file contains the styles for your website. It controls how things look, such as colors and layouts.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You don&amp;rsquo;t need to worry too much about these files yet. Over time, as you add more features, you&amp;rsquo;ll add more files.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="step-5-making-changes-to-your-project"&gt;Step 5: Making Changes to Your Project&lt;/h2&gt;
&lt;p&gt;Now, let&amp;rsquo;s modify your project to see how easy it is to work with Vite and TypeScript.&lt;/p&gt;
&lt;h3 id="51-changing-text-on-the-page"&gt;5.1 Changing Text on the Page&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open &lt;code&gt;index.html&lt;/code&gt; in your code editor.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Find the line that says:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Vite + TypeScript&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Change it to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, World!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Save the file.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When you go back to your browser (the one you opened in Step 3), you&amp;rsquo;ll see the text has changed without refreshing the page! This is one of Vite&amp;rsquo;s cool features—it automatically updates the page when you save changes.&lt;/p&gt;
&lt;h3 id="52-adding-some-interactivity"&gt;5.2 Adding Some Interactivity&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Open &lt;code&gt;main.ts&lt;/code&gt; in your editor.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the following code at the end of the file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-typescript" data-lang="typescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kr"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;button&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;Click Me&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;click&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;You clicked the button!&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Save the file.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;When you look at your browser again, you&amp;rsquo;ll see a button has appeared. If you click it, an alert will pop up! This is an example of how you can make your page interactive using TypeScript.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id="conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;ve now set up a project using Vite and TypeScript, made some changes, and added a button to your webpage. By using Vite, you can develop websites quickly, and with TypeScript, you can write code that&amp;rsquo;s safer and easier to manage.&lt;/p&gt;
&lt;h3 id="recap"&gt;Recap:&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vite&lt;/strong&gt; helps you develop faster by loading only what you need and refreshing changes automatically.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; helps catch potential errors before they happen, making your code more reliable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can now continue building your project by exploring more TypeScript features or adding more pages to your website.&lt;/p&gt;
&lt;p&gt;Happy coding!&lt;/p&gt;
]]></content:encoded><dc:creator>Rahat Zaman</dc:creator><media:content url="https://rahatzaman.me//images/thumbs/vite+typescript.png" medium="image"><media:title type="html">featured image</media:title></media:content><category>Vite</category><category>TypeScript</category><category>Psychology Experiments</category></item></channel></rss>