- Useful for preventing me from pushing the quick hacks I'm trying to commit in JavaScript.
- Modules you depend on may not have typescript definitions
- To avoid typing, you may have to go backwards
const ... = require()
- To avoid typing, you may have to go backwards
- Definition hell
Node | TypeScript Target | Support |
---|---|---|
6.17 | es5 | AWS Lambda Node v6.10 support |
es6 | ||
8.10-9.11.2 | es2016 | |
8.10-9.11.2 | es2017 | AWS Lambda Node v8.10 support |
10-nightly | es2018 | AWS Lambda Node v10.x support |
es2019 |
General Rules:
- If you are targeting for web set target to
es5
- If you targeting for AWS Lambda, set your target to
es2018
Node.js v10.x
supportses2018
- If you are targeting for local and using the latest node, go for
es2019
- I'm assuming you can target for
esnext
as well as it wouldn't make sense to write code that the latest Node.js could write...but I could be wrong
- I'm assuming you can target for
https://www.typescriptlang.org/docs/handbook/compiler-options.html
Enable:
declaration: true
when you want to share a Typescript code across modulesjsx: react
when you want to share a React component across modules
Enable in your compiler option:
declaration: true
jsx: react
https://mariusschulz.com/blog/typing-destructured-object-parameters-in-typescript
function Render(props: {uri?: string}) {
function toJSON(
value: any,
{ pretty }: { pretty: boolean }
) {
const indent = pretty ? 4 : 0;
return JSON.stringify(value, null, indent);
}
interface LRUHashMap {
[value: number]: DoublyLinkedList;
}
class LRUHash {
private hash: LRUHashMap;
construct() {
this.hash = {};
}
}
This fails:
const enum Key { ... }
const config: { [key: Key]: string } = {};
Keys can only be string
or number
.
Use Map
instead:
const enum Key { ... }
const config = new Map<Key, string>();
https://github.com/sw-yx/react-typescript-cheatsheet
interface Props {
children?: any;
}
Recommended microsoft/TypeScript#6471 (comment)
export default function UriField() {
const [uri, setURI] = useState<string | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
const onClick = () => {
if (!inputRef ||
!inputRef.current) {
return;
}
setURI(inputRef.current.value);
}
return (
<>
<input ref={inputRef} type="text" />
<input onClick={onClick} type="submit" value="Submit"/>
</>
);
}