Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to declare type of props in vuejs with typescript?

Here’s the snippet of code

<tr v-for="(log, index) in logs">
        <td>{{ index + 1 }}</td>
        <td>{{ log.timestamp }}</td>
        <td>{{ log.logType }}</td>
        <td>{{ log.logMessage }}</td>
      </tr>



defineProps({
  logs: {
    type: [Array],
    required: true
  }
})

Here log would be

{ timestamp : "2024-05-07 08:51:06",
logType : "INFO",
logMessage : "mailbox allocated for rsvp"
}

I’m new to typescript and I’m getting the below warning for log.timestamp, log.logType and log.logMessage during build

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘log’ is of type ‘unknown’.

What should be the type of logs here?

>Solution :

Assuming log is of type

type LogEntry = {
  timestamp: string;
  logType: string;
  logMessage: string;
};

as answered in your previous question.

You can do it

const props = defineProps({
  logs: {
    type: Array as PropType<Array<LogEntry>>,
    required: true,
  },
});

Don’t forget to import PropType.

import type { PropType } from 'vue';

Docs

If you didn’t have a type/interface for some reason you could declare it straight away like

logs: {
    type: Array as PropType<Array<{timestamp: string, logType: string, logMessage: string}>>,
    required: true,
  },
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading