Unionversity - Typescript course

Hi!

I´m stuck on task 13, Unionversity https://www.codecademy.com/courses/learn-typescript/projects/union-types-project
“includes doesnt exist on type string” I can´t see the problem .includes() shuld work on an array?

import courses from './courses';
import studyGroups from './studyGroups';

type Course = {
    id:           number,
    studyGroupId: number,
    title:        string,
    keywords:     string[],
    eventType:    string
}
type StudyGroup = {
    id: number,
    courseId: number,
    title: string,
    keywords: string[],
    eventType: string
}
type SearchEventOptions = {
  query: string | number,
  eventType: 'courses' | 'groups'
}

function searchEvents(options: SearchEventOptions ) {
  const events: (Course | StudyGroup)[] = options.eventType === 'courses' ? courses : studyGroups;

  events.filter((event: Course | StudyGroup) => {
    if(typeof options.query === 'number') {
      return options.query === event.id;           
    }
    if(typeof options.query === 'string') {
      return event.keywords.includes(options.query);
    }
  }) 
};

const searchResult = searchEvents({query: 'art', eventType: 'courses'});
console.log(searchResult);

Hello, and welcome to the forums!

I remember running into this when I did the project as well. It’s basically a linting issue in the learning environment editor. You can continue the project and it will still run fine.

Outside of the learning environment, setting the “lib” compiler option to “es2016” or later in tsconfig.json would make that go away. Making that same change to tsconfig.json in the learning environment doesn’t seem to take away that warning in the editor.

It still compiles fine though because the tsconfig.json we’re using targets es2017.

1 Like