mirror of
https://github.com/cupcakearmy/typescript-talk.git
synced 2024-12-22 08:06:31 +00:00
improve naming and fix small mistakes
This commit is contained in:
parent
a0768a8cd7
commit
e3e331ddf8
49
index.html
49
index.html
@ -139,7 +139,7 @@
|
||||
</ul>
|
||||
</section>
|
||||
<section>
|
||||
<h3>Type Cheking</h3>
|
||||
<h3>Type Checking</h3>
|
||||
|
||||
<ul>
|
||||
<li class="fragment">noImplicitAny</li>
|
||||
@ -577,20 +577,20 @@
|
||||
<section>
|
||||
<h3>Merging Objects together</h3>
|
||||
<pre class="fragment fade-in-then-semi-out"><code data-trim class="hljs">
|
||||
function merge<O, T>(data1: O, data2: T): O | T {
|
||||
function merge<O, T>(master: O, myFeatureBranchFrom3MonthsAgo: T): O | T {
|
||||
return {
|
||||
...data1,
|
||||
...data2,
|
||||
...master,
|
||||
...myFeatureBranchFrom3MonthsAgo,
|
||||
};
|
||||
}
|
||||
</code></pre>
|
||||
<pre class="fragment fade-in-then-semi-out"><code data-trim class="hljs">
|
||||
type Merge<O, T> = Omit<O, keyof T> & T;
|
||||
|
||||
function merge<O, T>(data1: O, data2: T): Merge<O, T> {
|
||||
function merge<O, T>(master: O, myFeatureBranchFrom3MonthsAgo: T): Merge<O, T> {
|
||||
return {
|
||||
...data1,
|
||||
...data2,
|
||||
...master,
|
||||
...myFeatureBranchFrom3MonthsAgo,
|
||||
};
|
||||
}
|
||||
</code></pre>
|
||||
@ -598,25 +598,30 @@
|
||||
<section>
|
||||
<h3>Distinguish union types</h3>
|
||||
<pre><code data-trim class="hljs">
|
||||
interface TypeA {
|
||||
type: 'A';
|
||||
enum Type {
|
||||
A = 'A',
|
||||
B = 'B',
|
||||
}
|
||||
|
||||
interface ImagineCoolExampleName {
|
||||
type: Type.A;
|
||||
foo: string;
|
||||
bar: string;
|
||||
}
|
||||
|
||||
interface TypeB {
|
||||
type: 'B';
|
||||
interface ImTooLazyToThinkOfRealWorldExamples {
|
||||
type: Type.B;
|
||||
foo: number;
|
||||
blubb: string;
|
||||
}
|
||||
|
||||
function distinguishType(type: TypeA | TypeB) {
|
||||
if (type.type === 'A') {
|
||||
type.foo.includes('whatever');
|
||||
type.bar;
|
||||
function distinguishType(obj: ImagineCoolExampleName | ImTooLazyToThinkOfRealWorldExamples) {
|
||||
if (obj.type === Type.A) {
|
||||
obj.foo.includes('whatever');
|
||||
obj.bar;
|
||||
} else {
|
||||
type.blubb.includes('something');
|
||||
type.foo.toFixed(2);
|
||||
obj.blubb.includes('something');
|
||||
obj.foo.toFixed(2);
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
@ -632,8 +637,8 @@
|
||||
2: 'c',
|
||||
};
|
||||
|
||||
function filterByKeys(keys: (keyof typeof someObj)[]) {
|
||||
keys.map(key => someObj[key]);
|
||||
function filterByKeys(keys: (keyof typeof someObj)[]): ((typeof someObj)[keyof typeof someObj])[] {
|
||||
return keys.map(key => someObj[key]);
|
||||
}
|
||||
|
||||
filterByKeys(Object.keys(someObj)); // error
|
||||
@ -684,8 +689,8 @@
|
||||
2: 'c',
|
||||
};
|
||||
|
||||
function filterByKeys(keys: (keyof typeof someObj)[]) {
|
||||
keys.map(key => someObj[key]);
|
||||
function filterByKeys(keys: (keyof typeof someObj)[]): ((typeof someObj)[keyof typeof someObj])[] {
|
||||
return keys.map(key => someObj[key]);
|
||||
}
|
||||
|
||||
function getKeysFromObject<
|
||||
@ -801,4 +806,4 @@
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user