#Create custom TypeScript types and make the available globally

48 messages · Page 1 of 1 (latest)

delicate grotto
#

Hello, as you can read in the Title I want to create custom TypeScript Types. In React I had a folder names "types" in the root Directory. And while I can do the same thing here and also import them, once I reload the Page I get '/_nuxt/types/Message.ts' does not provide an export named 'Message' is ther a way to fix this?

limpid pivot
#

Check the 1 answer and see if it can suit you

delicate grotto
#

How would I import that? Importing it like this: import {Message} from '~/types/ doesn't work

delicate grotto
limpid pivot
#

It should auto import

delicate grotto
limpid pivot
#

Interesting my file looks identical and I can use my types globally

#

With no import

delicate grotto
#

With no import ahh okay, let me try

#

Ahh maybe its caused by me using it in defineProps? ts const input = defineProps({ msg: { type: Message, required: true, });

limpid pivot
#

So that doesn't work?

delicate grotto
#

not entirely sure I will try now

#

it does complain tho that I am using a type as a value tho

limpid pivot
#

I usually don't do the way you did there

#

Give me just a sec

#

I'm on mobile so hard to do example one sec

delicate grotto
#

ReferenceError: Message is not defined

limpid pivot
#

Sorry let me hop on pc

#

2 min

delicate grotto
#

Take your time

limpid pivot
#

so couple ways here

#

it seems you are using ts but then not at the same time

#

using like the required stuff

#

so something like

#

can you try

#
const input = defineProps<{ msg: Message }>```
#

by default this is required if you wanted it optional you would just use msg?: Message

delicate grotto
limpid pivot
#

hm

#

try this way instead and see if you get same error

#
type Props = {
    msg: Message
}

const input = defineProps<Props>()```
#

should really be the same

delicate grotto
#

That looks like it works 😄

#

Thanks alot!

limpid pivot
#

and just a heads up

#

I wish it was possible but it's not yet you can't do like defineProps<Message>

#

it has been requested that they allow types like that to be used but not yet

delicate grotto
#

that's fine 😄

limpid pivot
#

so just make sure you do a interface or type that includes your custom types

delicate grotto
#

Are you active on this Discord? in case I need other help

limpid pivot
#

I try to be

#

you can always dm me

delicate grotto
#

That would be awesome 🙂

limpid pivot
#

so the way i put above or ```ts
interface Props {
msg: Message
}

const input = defineProps<Props>()```

#

is the other option using interface

#

anyways, if you need anymore help just shoot me a dm and ill get back to you

delicate grotto
#

Thanks alot! You were a great help