options-test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import Vue, { VNode } from "../index";
  2. import { AsyncComponent, ComponentOptions, FunctionalComponentOptions, Component } from "../index";
  3. import { CreateElement } from "../vue";
  4. interface MyComponent extends Vue {
  5. a: number;
  6. }
  7. const option: ComponentOptions<MyComponent> = {
  8. data() {
  9. return {
  10. a: 123
  11. }
  12. }
  13. }
  14. // contravariant generic should use never
  15. const anotherOption: ComponentOptions<never> = option
  16. const componentType: Component = option
  17. Vue.component('sub-component', {
  18. components: {
  19. a: Vue.component(""),
  20. b: {}
  21. }
  22. });
  23. Vue.component('prop-component', {
  24. props: {
  25. size: Number,
  26. name: {
  27. type: String,
  28. default: '0',
  29. required: true,
  30. }
  31. },
  32. data() {
  33. return {
  34. fixedSize: this.size.toFixed(),
  35. capName: this.name.toUpperCase()
  36. }
  37. }
  38. });
  39. Vue.component('string-prop', {
  40. props: ['size', 'name'],
  41. data() {
  42. return {
  43. fixedSize: this.size.whatever,
  44. capName: this.name.isany
  45. }
  46. }
  47. });
  48. class User {
  49. private u = 1
  50. }
  51. class Cat {
  52. private u = 1
  53. }
  54. Vue.component('union-prop', {
  55. props: {
  56. primitive: [String, Number],
  57. object: [Cat, User],
  58. regex: RegExp,
  59. mixed: [RegExp, Array],
  60. union: [User, Number] as {new(): User | Number}[] // requires annotation
  61. },
  62. data() {
  63. this.primitive;
  64. this.object;
  65. this.union;
  66. this.regex.compile;
  67. this.mixed;
  68. return {
  69. fixedSize: this.union,
  70. }
  71. }
  72. });
  73. Vue.component('component', {
  74. data() {
  75. this.$mount
  76. this.size
  77. return {
  78. a: 1
  79. }
  80. },
  81. props: {
  82. size: Number,
  83. name: {
  84. type: String,
  85. default: '0',
  86. required: true,
  87. }
  88. },
  89. propsData: {
  90. msg: "Hello"
  91. },
  92. computed: {
  93. aDouble(): number {
  94. return this.a * 2;
  95. },
  96. aPlus: {
  97. get(): number {
  98. return this.a + 1;
  99. },
  100. set(v: number) {
  101. this.a = v - 1;
  102. },
  103. cache: false
  104. }
  105. },
  106. methods: {
  107. plus() {
  108. this.a++;
  109. this.aDouble.toFixed();
  110. this.aPlus = 1;
  111. this.size.toFixed();
  112. }
  113. },
  114. watch: {
  115. 'a': function(val: number, oldVal: number) {
  116. console.log(`new: ${val}, old: ${oldVal}`);
  117. },
  118. 'b': 'someMethod',
  119. 'c': {
  120. handler(val, oldVal) {
  121. this.a = val
  122. },
  123. deep: true
  124. }
  125. },
  126. el: "#app",
  127. template: "<div>{{ message }}</div>",
  128. render(createElement) {
  129. return createElement("div", {
  130. attrs: {
  131. id: "foo"
  132. },
  133. props: {
  134. myProp: "bar"
  135. },
  136. domProps: {
  137. innerHTML: "baz"
  138. },
  139. on: {
  140. click: new Function
  141. },
  142. nativeOn: {
  143. click: new Function
  144. },
  145. class: {
  146. foo: true,
  147. bar: false
  148. },
  149. style: {
  150. color: 'red',
  151. fontSize: '14px'
  152. },
  153. key: 'myKey',
  154. ref: 'myRef'
  155. }, [
  156. createElement(),
  157. createElement("div", "message"),
  158. createElement(Vue.component("component")),
  159. createElement({} as ComponentOptions<Vue>),
  160. createElement({
  161. functional: true,
  162. render(c: CreateElement) {
  163. return createElement()
  164. }
  165. }),
  166. createElement(() => Vue.component("component")),
  167. createElement(() => ( {} as ComponentOptions<Vue> )),
  168. createElement((resolve, reject) => {
  169. resolve({} as ComponentOptions<Vue>);
  170. reject();
  171. }),
  172. "message",
  173. [createElement("div", "message")]
  174. ]);
  175. },
  176. staticRenderFns: [],
  177. beforeCreate() {
  178. (this as any).a = 1;
  179. },
  180. created() {},
  181. beforeDestroy() {},
  182. destroyed() {},
  183. beforeMount() {},
  184. mounted() {},
  185. beforeUpdate() {},
  186. updated() {},
  187. activated() {},
  188. deactivated() {},
  189. errorCaptured() {
  190. return true
  191. },
  192. directives: {
  193. a: {
  194. bind() {},
  195. inserted() {},
  196. update() {},
  197. componentUpdated() {},
  198. unbind() {}
  199. },
  200. b(el, binding, vnode, oldVnode) {
  201. el.textContent;
  202. binding.name;
  203. binding.value;
  204. binding.oldValue;
  205. binding.expression;
  206. binding.arg;
  207. binding.modifiers["modifier"];
  208. }
  209. },
  210. components: {
  211. a: Vue.component(""),
  212. b: {} as ComponentOptions<Vue>
  213. },
  214. transitions: {},
  215. filters: {
  216. double(value: number) {
  217. return value * 2;
  218. }
  219. },
  220. parent: new Vue,
  221. mixins: [Vue.component(""), ({} as ComponentOptions<Vue>)],
  222. name: "Component",
  223. extends: {} as ComponentOptions<Vue>,
  224. delimiters: ["${", "}"]
  225. });
  226. Vue.component('provide-inject', {
  227. provide: {
  228. foo: 1
  229. },
  230. inject: {
  231. injectFoo: 'foo',
  232. injectBar: Symbol(),
  233. injectBaz: { from: 'baz' },
  234. injectQux: { default: 1 },
  235. injectQuux: { from: 'quuz', default: () => ({ value: 1 })}
  236. }
  237. })
  238. Vue.component('provide-function', {
  239. provide: () => ({
  240. foo: 1
  241. })
  242. })
  243. Vue.component('component-with-scoped-slot', {
  244. render (h) {
  245. interface ScopedSlotProps {
  246. msg: string
  247. }
  248. return h('div', [
  249. h('child', [
  250. // default scoped slot as children
  251. (props: ScopedSlotProps) => [h('span', [props.msg])]
  252. ]),
  253. h('child', {
  254. scopedSlots: {
  255. // named scoped slot as vnode data
  256. item: (props: ScopedSlotProps) => [h('span', [props.msg])]
  257. }
  258. })
  259. ])
  260. },
  261. components: {
  262. child: {
  263. render (this: Vue, h: CreateElement) {
  264. return h('div', [
  265. this.$scopedSlots['default']({ msg: 'hi' }),
  266. this.$scopedSlots['item']({ msg: 'hello' })
  267. ])
  268. }
  269. }
  270. }
  271. })
  272. Vue.component('narrow-array-of-vnode-type', {
  273. render (h): VNode {
  274. const slot = this.$scopedSlots.default({})
  275. if (typeof slot !== 'string') {
  276. const first = slot[0]
  277. if (!Array.isArray(first) && typeof first !== 'string') {
  278. return first;
  279. }
  280. }
  281. return h();
  282. }
  283. })
  284. Vue.component('functional-component', {
  285. props: ['prop'],
  286. functional: true,
  287. inject: ['foo'],
  288. render(createElement, context) {
  289. context.props;
  290. context.children;
  291. context.slots();
  292. context.data;
  293. context.parent;
  294. return createElement("div", {}, context.children);
  295. }
  296. });
  297. Vue.component('functional-component-object-inject', {
  298. functional: true,
  299. inject: {
  300. foo: 'foo',
  301. bar: Symbol(),
  302. baz: { from: 'baz' },
  303. qux: { default: 1 },
  304. quux: { from: 'quuz', default: () => ({ value: 1 })}
  305. },
  306. render(h) {
  307. return h('div')
  308. }
  309. })
  310. Vue.component("async-component", ((resolve, reject) => {
  311. setTimeout(() => {
  312. resolve(Vue.component("component"));
  313. }, 0);
  314. return new Promise((resolve) => {
  315. resolve({
  316. functional: true,
  317. render(h: CreateElement) { return h('div') }
  318. });
  319. })
  320. }));
  321. Vue.component('async-es-module-component', () => import('./es-module'))