Skip to content

Instantly share code, notes, and snippets.

@MaxMonteil
Last active July 9, 2025 14:53
Show Gist options
  • Select an option

  • Save MaxMonteil/fc3008bd2717570481bbd9caeb3340c7 to your computer and use it in GitHub Desktop.

Select an option

Save MaxMonteil/fc3008bd2717570481bbd9caeb3340c7 to your computer and use it in GitHub Desktop.
Basic Vue interview
<template>
<h1>List items</h1>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
<button @click="addItem">Add item</button>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([
{ name: "Item 1", id: 1 },
{ name: "Item 2", id: 2 },
{ name: "Item 3", id: 3 },
])
function addItem() {
const id = items.length + 1
items.push({ name: `Item ${id}`, id })
}
</script>
@MaxMonteil
Copy link
Author

React version:

export default function App() {
  const items = [
    { name: "Item 1", id: 1 },
    { name: "Item 2", id: 2 },
    { name: "Item 3", id: 3 },
  ];

  const addItem = () => {
    const id = items.length + 1;
    items.push({ name: `Item ${id}`, id })
  };

  return (
    <div>
      <h1>List items</h1>
      <ul>
        {items.map((item) => (
          <li key={item.id}>{item.name}</li>
        ))}
      </ul>
      <button onClick={addItem}>Add item</button>
    </div>
  );
}

@MaxMonteil
Copy link
Author

With types

<template>
  <h1>List items</h1>

  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
    </li>
  </ul>
  <button @click="addItem">Add item</button>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// 1. Define this type such that given objects will require an `id` parameter
// 2. Make the type of the `id` parameter configurable with a second generic argument
declare type SafeItem<T>;

type ListItem = { name: string }

const items = ref<SafeItem<ListItem>[]>([
  { name: "Item 1", id: 1 },
  { name: "Item 2", id: 2 },
  { name: "Item 3", id: 3 },
])

function addItem() {
  const id = items.length + 1
  items.push({ name: `Item ${id}`, id })
}
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment