Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types for Typescript users of react-quiz-component (.d.ts) #273

Open
d3vAdv3ntur3s opened this issue May 30, 2024 · 4 comments · May be fixed by #352
Open

Types for Typescript users of react-quiz-component (.d.ts) #273

d3vAdv3ntur3s opened this issue May 30, 2024 · 4 comments · May be fixed by #352

Comments

@d3vAdv3ntur3s
Copy link

d3vAdv3ntur3s commented May 30, 2024

Nice job with the library, I was trialing the component, doesn't quite fit for my use case, but I thought it might be useful to have some Types to assist for Typescript users. Quickly put this together whilst I was trying out the component, hopefully helpful to you if not onlookers.

File: react-quiz-component.d.ts

    declare module 'react-quiz-component' {
      import { ReactElement } from 'react';
      
      type QuestionType = "text" | "photo"
      
      type AnswerSelectionType = "single" | "multiple"
  
      type Question = {
          question: string
          questionType: QuestionType
          answers: string[]
          correctAnswer: string
          answerSelectionType: AnswerSelectionType
          messageForCorrectAnswer?: string
          messageForIncorrectAnswer?: string
          explanation?: string
          point?: string
          segment?: string
      }
  
      type QuizStructure = {
          quizTitle: string
          questions: question[]
      }
  
      type QuizProps = {
          quiz: QuizStructure;
          disableSynopsis?: boolean;
          shuffleAnswer?: boolean;
          shuffle?: boolean;
          showDefaultResult?: boolean;
          showInstantFeedback?: boolean;
          continueTillCorrect?: boolean;
          timer?: number;
          allowPauseTimer?: boolean
      }
  
      const Quiz: (props: QuizProps) => ReactElement;
      export default Quiz;
  }
`
@diegoroccia
Copy link

@d3vAdv3ntur3s thank you for this! I am using this library in one project of mine and I want to add the types. can I do it outside of the library?

@d3vAdv3ntur3s
Copy link
Author

@diegoroccia Yes that's what I've done here if I'm understanding what you mean.

The declare module just needs to match the name of the library that you are adding the types for.

Good luck ^_^

@Julix91
Copy link

Julix91 commented Oct 26, 2024

Couple typos: at the end, lowercaseq` in Questions - but I'll make a PR to add this to the project (and the typing in the package.json to point at the new file).

How big a lift do you think it would be to convert the whole project to typescript? Maybe worth doing that vs just typing.

@julix-unity julix-unity linked a pull request Oct 26, 2024 that will close this issue
@julix-unity
Copy link

Also see https://www.danielfullstack.com/article/stop-using-d-ts-files-in-typescript - ideally we'd make the whole thing TypeScript and have it auto generate the .d.ts files with a source map so you can still get straight back to the ts implementation if you want to see how something works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants