Created
October 22, 2019 10:41
-
-
Save dhcmrlchtdj/fc39b71485e083b2a15352381c1c95d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://www.typescriptlang.org/play/?ssl=1&ssc=1&pln=64&pc=1#code/PTAEjgVQjdMCldF-FQHU0Fxyho+QFAlIN0VBrcoTu1AWaoBD-8gI36DCcoM+BgL2aCwmoELmgIW6DLfoCHmgkP+D6coPRmgAHKDsFg0DKCYDAXdGEDyyoA3lQIAMgMATJgGH-ATdEMuoAEIB7AB4AeAEYBDAM4BTAHyhAMSoadu4wFd9lwFcqgKjlAskZr9OgPpGzMVs9AItQbz9QhUBGHS4pIK09R2dQSUAEI1AfbX8TUxiueSCAWhKgwAG5QAk5QGvlTNyFQEAY0ABLADtjABdDFoBjU1BALE162NrAjEALCMAG50QFQFLjQDZTEb6GQAZ1QE7TBUB75UBTuUBrDVBk4tKMCkA4uVAAa012gBtW0EBpOUBvHwZK5pbgTQd2oO2mQAgVBAMZCABW1AKbmwkAx5EKQBDyvA3qB5ApACvxgD21QA8CqBPu1QPFDkVUKh2gBPAAOfVCoAAvPt2gAnVoAc1AAB9QC0HABbfSmWmE0l9ZJUmn0loMgkYQCf2qi0VJQN1NEUAG6GeldXp8slyzQ5MxCgAUAEoqZZQhq+vLfILqYbjfsnOKwBLYfItS06YYlSqmmrTGbXe6dX1rQATQydABcdv0RsplnZ12ufvlbtphktTn1oYjixjcYcCaC3VyACYk5oU2nQsXM2HDJHkrnFmWK+n9NWQ7XI6FGwcgq1Paqer7iZrWoHfAANGvZ7u200jvpj5KT6d1qM9+14gnJjqgABmAEZIxaKdbGwAGVA77F74vH7VW0A22OgADkr+35d37VMHSPWsDIVbyvL9sR-Do7wAx9D1ADAABV+TfE9clfZpjDZK5QBMYwmgZFpDH0a4+naTRQAXJCHycD8txAtobwAZnvFtTyfLM1wbW140Ta99wAFiYgNHz1Niu1yRsuM-OiyN-dpGP9VNAOpPdeNo78ZP4+S02g+jYLABDNVfZMA1CVCmnQlpMOw3D8MI4jSPIwzy0EqiHVAEp8VUm8AFYBIUqtV3rJwDVE3UX0vHi9wANl8rSnHbVjOxzQL9Ftd9JLUjofM0wN4qizzpI6aLsuSXKvN00B9L6RzmJLUzzMs4wcLwgiiLI+zEOq5y22ojB3PSm8AHZIzHUIVw7GcxJCoNLHC0D9wADmGlpWzGhLswbZLUuonjwPaIa3nHKclPm8rKrfJcnEnOqMOxKzmtstqyI6kbciu-LdsWg7lyO-cBtO57ltGidros27Guslq7KegyLv0N6gA | |
| // --- | |
| // 后面用函数为例 | |
| // 其他容器(数组乃至更复杂的结构)也是一样的道理 | |
| // 协变、逆变,讲的是 Box<base> 和 Box<sub> 哪个才是 box_base | |
| // Box<base> 是 box_base,就是协变 | |
| // Box<sub> 变成 box_base,就是逆变 | |
| // --- | |
| // 什么叫 base,谁 instance 多谁就是 base | |
| // 表现为,接收 base 的地方,可以填 sub | |
| // --- | |
| // 至于 kotlin 之类的什么 in/out | |
| // 可以结合函数的例子来理解,参数 in 逆变,返回值 out 协变 | |
| // --- | |
| type base = string | number | |
| type sub = string | |
| // 对返回值协变 co-variance | |
| type co_base = () => base | |
| type co_sub = () => sub | |
| // 对参数逆变 contra-variance | |
| type contra_base = (data: sub) => null | |
| type contra_sub = (data: base) => null | |
| // case2 | |
| type contra_base2 = (data: sub) => base | |
| type contra_sub2 = (data: base) => sub | |
| // in-variance | |
| type in_base_X = (data: base) => base | |
| type in_sub_X = (data: sub) => sub | |
| // --- | |
| const f1: co_base = () => 0 | |
| const f2: co_sub = () => '' | |
| const test1: co_base = f2 | |
| const test2: co_sub = f1 // Type 'co_base' is not assignable to type 'co_sub' | |
| // --- | |
| const f3: contra_base = (data: sub) => null | |
| const f4: contra_sub = (data: base) => null | |
| const test3: contra_base = f4 | |
| const test4: contra_sub = f3 // Type 'contra_base' is not assignable to type 'contra_sub' | |
| // --- | |
| const f5: contra_base2 = (data: sub): base => 0 | |
| const f6: contra_sub2 = (data: base): sub => '' | |
| const test5: contra_base2 = f6 | |
| const test6: contra_sub2 = f5 // Type 'contra_base2' is not assignable to type 'contra_sub2' | |
| // --- | |
| const f7: in_base_X = (data: base): base => 0 | |
| const f8: in_sub_X = (data: sub): sub => '' | |
| const test7: in_base_X = f8 // Type 'in_sub_X' is not assignable to type 'in_base_X' | |
| const test8: in_sub_X = f7 // Type 'in_base_X' is not assignable to type 'in_sub_X' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment