In frontend design systems, presentation components-small, focused components responsible solely for displaying data—are the building blocks of clean UI architecture. However, when these components accept raw arrays without strict contracts, optional fields like social links, profile URLs, and handles can easily lead to template errors or broken links.

In my recent contribution to GitLab's Contributors Platform (MR !3194), I migrated the ContributorSocialLinks component and its test suite to strict TypeScript.

Here is a quick breakdown of how typing lightweight presentation components improves maintainability and test reliability.

1. The Challenge: Loose Array Props

Originally, ContributorSocialLinks.vue accepted contributors through standard runtime prop validation:

<!-- Before (JavaScript) --> <script setup> const props = defineProps({ contributors: { type: Array, required: true, }, }); const contributorsWithSocials = props.contributors.filter( contributor => contributor.discord || contributor.linkedIn ); </script> 

Why was this problematic?

  1. Missing Shape Contract: The compiler had no knowledge of what attributes a "contributor" held. A parent passing an object with linkedin (lowercase 'i') or discord_handle would fail silently.
  2. Untyped Filter Results: contributorsWithSocials held elements of type any, preventing type checking across the component template (such as icon rendering or URL generation).

2. Strong Type Contracts with interface

By adding lang="ts" and exporting a discrete Contributor interface, we created a clear contract for both the component and its consumers:

<!-- After (TypeScript) --> <script setup lang="ts"> export interface Contributor { name: string; gitlabUsername?: string; linkedIn?: string; discord?: string; } const props = defineProps<{ contributors: Contributor[]; }>(); const contributorsWithSocials = props.contributors.filter( contributor => contributor.discord || contributor.linkedIn ); </script> 

Advantages:

  • Type-Only defineProps: Eliminates verbose runtime object definitions. Vue 3 automatically validates props at compile time based on the TypeScript definition.
  • Reusable Interface: Parent components (e.g., contributor lists, award cards) can import type { Contributor } directly to ensure prop compatibility.
  • Safe Filtering: TypeScript's type checker now verifies that .discord and .linkedIn exist on the Contributor model.

3. Strict Type Safety in Unit Tests

In ContributorSocialLinks.spec.ts, untyped factory helpers were replaced with typed parameters using the exported interface:

import { shallowMount } from '@vue/test-utils'; import type { Contributor } from './ContributorSocialLinks.vue'; import ContributorSocialLinks from './ContributorSocialLinks.vue'; describe('ContributorSocialLinks', () => { const createWrapper = (contributors: Contributor[]) => shallowMount(ContributorSocialLinks, { props: { contributors }, }); it('does not render when no contributors have socials', () => { const wrapper = createWrapper([ { name: 'Test User' }, // Fully conforms to Contributor interface ]); expect(wrapper.find('p').exists()).toBe(false); }); }); 

Spec assertions are now resilient against future schema changes, ensuring that mock objects always mirror realistic application data.

Key Takeaways

  1. Export Component Prop Interfaces: When a component receives structured domain data (like a user, contributor, or product), export its interface so parent views and test suites remain in sync.
  2. Favor Type-Only defineProps: defineProps<{ data: T }>() is cleaner and offers better IDE autocompletion compared to JavaScript options-based prop validators.
  3. Keep Spec Helpers Strongly Typed: Passing typed parameters into helper functions like createWrapper prevents accidental drift between test fixtures and production models.

Check out the merged Merge Request on GitLab:

👉 GitLab Contributors MR !3194

Author: Manish Kumar

GitLab: @manishpatel00