You can stick types to JavaScript code, that's named "TypeScript Type Declaration", and the extension is .d.ts. When TypeScript (or also an editor with intellisense) sees a javascript file and a .d.ts file with the same name, it will import the type definitions from that file.
There's a project that aims to provide type definitions for every JavaScript library, maybe you've heard talking about DefinitelyTyped.
For example:
// example.js
function hello(name) {
console.log(`Hello ${name}!`);
}
// example.d.ts
function hello(name: string);
// main.ts
import { hello } from 'example';
hello(42); // Error! "name" must be a string
The same system has been used to provide types in React
Answer from Christian Vincenzo Traina on Stack OverflowYou can stick types to JavaScript code, that's named "TypeScript Type Declaration", and the extension is .d.ts. When TypeScript (or also an editor with intellisense) sees a javascript file and a .d.ts file with the same name, it will import the type definitions from that file.
There's a project that aims to provide type definitions for every JavaScript library, maybe you've heard talking about DefinitelyTyped.
For example:
// example.js
function hello(name) {
console.log(`Hello ${name}!`);
}
// example.d.ts
function hello(name: string);
// main.ts
import { hello } from 'example';
hello(42); // Error! "name" must be a string
The same system has been used to provide types in React
React source code is written in Flow. The file extension is still .js, and there are types in those files. If Flow isn't installed, VS Code doesn't understand what it is and just reports a bunch of bugs.
Videos
Hello!
I'm searching for a Typescript-written React project, preferably on GitHub, to study and learn from. It would be fantastic if it were designed and written properly. I'm aware that there are other examples available, but I'm looking for something more complex.
Thanks.