Say I have a file user.ts that has the following declaration:
export interface User {
userName: string;
}
I want to import this interface from another ts script, but since JavaScript doesn't support interfaces the transpiler wouldn't produce the user.js and I'm getting an error on import.
Of course, I could declare User as a class as a workaround, but is there a way to declare an interface in a separate TypeScript file?
You can try making the user file have the .d.ts extension.
For example:
user.d.ts
export interface User {
name: string
}
and you can import it as usual:
someotherfile.ts
import {User} from './user';
function check(user: User) {
if (user.name) {
console.log('yeey works');
}
}
check({ name: 'ion' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With