options-test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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('prop-with-primitive-default', {
  74. props: {
  75. id: {
  76. type: String,
  77. default: () => String(Math.round(Math.random() * 10000000))
  78. }
  79. },
  80. created() {
  81. this.id;
  82. }
  83. });
  84. Vue.component('component', {
  85. data() {
  86. this.$mount
  87. this.size
  88. return {
  89. a: 1
  90. }
  91. },
  92. props: {
  93. size: Number,
  94. name: {
  95. type: String,
  96. default: '0',
  97. required: true,
  98. }
  99. },
  100. propsData: {
  101. msg: "Hello"
  102. },
  103. computed: {
  104. aDouble(): number {
  105. return this.a * 2;
  106. },
  107. aPlus: {
  108. get(): number {
  109. return this.a + 1;
  110. },
  111. set(v: number) {
  112. this.a = v - 1;
  113. },
  114. cache: false
  115. }
  116. },
  117. methods: {
  118. plus() {
  119. this.a++;
  120. this.aDouble.toFixed();
  121. this.aPlus = 1;
  122. this.size.toFixed();
  123. }
  124. },
  125. watch: {
  126. 'a': function(val: number, oldVal: number) {
  127. console.log(`new: ${val}, old: ${oldVal}`);
  128. },
  129. 'b': 'someMethod',
  130. 'c': {
  131. handler(val, oldVal) {
  132. this.a = val
  133. },
  134. deep: true
  135. }
  136. },
  137. el: "#app",
  138. template: "<div>{{ message }}</div>",
  139. render(createElement) {
  140. return createElement("div", {
  141. attrs: {
  142. id: "foo"
  143. },
  144. props: {
  145. myProp: "bar"
  146. },
  147. domProps: {
  148. innerHTML: "baz"
  149. },
  150. on: {
  151. click: new Function
  152. },
  153. nativeOn: {
  154. click: new Function
  155. },
  156. class: {
  157. foo: true,
  158. bar: false
  159. },
  160. style: {
  161. color: 'red',
  162. fontSize: '14px'
  163. },
  164. key: 'myKey',
  165. ref: 'myRef'
  166. }, [
  167. createElement(),
  168. createElement("div", "message"),
  169. createElement(Vue.component("component")),
  170. createElement({} as ComponentOptions<Vue>),
  171. createElement({
  172. functional: true,
  173. render(c: CreateElement) {
  174. return createElement()
  175. }
  176. }),
  177. createElement(() => Vue.component("component")),
  178. createElement(() => ( {} as ComponentOptions<Vue> )),
  179. createElement((resolve, reject) => {
  180. resolve({} as ComponentOptions<Vue>);
  181. reject();
  182. }),
  183. "message",
  184. [createElement("div", "message")]
  185. ]);
  186. },
  187. staticRenderFns: [],
  188. beforeCreate() {
  189. (this as any).a = 1;
  190. },
  191. created() {},
  192. beforeDestroy() {},
  193. destroyed() {},
  194. beforeMount() {},
  195. mounted() {},
  196. beforeUpdate() {},
  197. updated() {},
  198. activated() {},
  199. deactivated() {},
  200. errorCaptured(err, vm, info) {
  201. err.message
  202. vm.$emit('error')
  203. info.toUpperCase()
  204. return true
  205. },
  206. directives: {
  207. a: {
  208. bind() {},
  209. inserted() {},
  210. update() {},
  211. componentUpdated() {},
  212. unbind() {}
  213. },
  214. b(el, binding, vnode, oldVnode) {
  215. el.textContent;
  216. binding.name;
  217. binding.value;
  218. binding.oldValue;
  219. binding.expression;
  220. binding.arg;
  221. binding.modifiers["modifier"];
  222. }
  223. },
  224. components: {
  225. a: Vue.component(""),
  226. b: {} as ComponentOptions<Vue>
  227. },
  228. transitions: {},
  229. filters: {
  230. double(value: number) {
  231. return value * 2;
  232. }
  233. },
  234. parent: new Vue,
  235. mixins: [Vue.component(""), ({} as ComponentOptions<Vue>)],
  236. name: "Component",
  237. extends: {} as ComponentOptions<Vue>,
  238. delimiters: ["${", "}"]
  239. });
  240. Vue.component('provide-inject', {
  241. provide: {
  242. foo: 1
  243. },
  244. inject: {
  245. injectFoo: 'foo',
  246. injectBar: Symbol(),
  247. injectBaz: { from: 'baz' },
  248. injectQux: { default: 1 },
  249. injectQuux: { from: 'quuz', default: () => ({ value: 1 })}
  250. }
  251. })
  252. Vue.component('provide-function', {
  253. provide: () => ({
  254. foo: 1
  255. })
  256. })
  257. Vue.component('component-with-scoped-slot', {
  258. render (h) {
  259. interface ScopedSlotProps {
  260. msg: string
  261. }
  262. return h('div', [
  263. h('child', [
  264. // default scoped slot as children
  265. (props: ScopedSlotProps) => [h('span', [props.msg])]
  266. ]),
  267. h('child', {
  268. scopedSlots: {
  269. // named scoped slot as vnode data
  270. item: (props: ScopedSlotProps) => [h('span', [props.msg])]
  271. }
  272. })
  273. ])
  274. },
  275. components: {
  276. child: {
  277. render (this: Vue, h: CreateElement) {
  278. return h('div', [
  279. this.$scopedSlots['default']({ msg: 'hi' }),
  280. this.$scopedSlots['item']({ msg: 'hello' })
  281. ])
  282. }
  283. }
  284. }
  285. })
  286. Vue.component('narrow-array-of-vnode-type', {
  287. render (h): VNode {
  288. const slot = this.$scopedSlots.default({})
  289. if (typeof slot !== 'string') {
  290. const first = slot[0]
  291. if (!Array.isArray(first) && typeof first !== 'string') {
  292. return first;
  293. }
  294. }
  295. return h();
  296. }
  297. })
  298. Vue.component('functional-component', {
  299. props: ['prop'],
  300. functional: true,
  301. inject: ['foo'],
  302. render(createElement, context) {
  303. context.props;
  304. context.children;
  305. context.slots();
  306. context.data;
  307. context.parent;
  308. context.listeners.click;
  309. return createElement("div", {}, context.children);
  310. }
  311. });
  312. Vue.component('functional-component-object-inject', {
  313. functional: true,
  314. inject: {
  315. foo: 'foo',
  316. bar: Symbol(),
  317. baz: { from: 'baz' },
  318. qux: { default: 1 },
  319. quux: { from: 'quuz', default: () => ({ value: 1 })}
  320. },
  321. render(h) {
  322. return h('div')
  323. }
  324. })
  325. Vue.component('functional-component-check-optional', {
  326. functional: true
  327. })
  328. Vue.component("async-component", ((resolve, reject) => {
  329. setTimeout(() => {
  330. resolve(Vue.component("component"));
  331. }, 0);
  332. return new Promise((resolve) => {
  333. resolve({
  334. functional: true,
  335. render(h: CreateElement) { return h('div') }
  336. });
  337. })
  338. }));
  339. Vue.component('async-es-module-component', () => import('./es-module'))