Comments (6)
Let me boil it down. It seems we have two approaches to achieve type safety in template, if it is what you asked in this issue.
- tsx approach
- transpiling template approach
The tsx approach is more promising. The basic idea is writing vue component in tsx, transform it to jsx by tsc, and finally transpile it to plain js by babel, using this plugin. This setup reuses all existing resources and is the easiest way to achieve partial safety in vue jsx.
Before we start, please first read this post to understand how tsx is checked. https://www.typescriptlang.org/docs/handbook/jsx.html
You first need to declare JSX
namespace like this
declare namespace JSX {
interface ElementAttributesProperty {
props: ; // specify the property name to use
}
}
Then you can declare vue's class.
import {Component, Vue} from 'av-ts'
@Component
class MyComponent extends Vue {
// specify the property on the element instance type
props: {
foo: string;
}
}
// element attributes type for 'MyComponent' is '{foo?: string}'
var a = <MyComponent foo="+1s" />
Over. All you can check is props and instance property reference in TSX. custom events/domProps/native events is not support. This is because tsx does not support prefix like on-click
or domProps
. But they are used by vue's babel transformer. https://github.com/vuejs/babel-plugin-transform-vue-jsx
from av-ts.
Now it's template transpiling approach. This pathway should achieve more integrated result, say, checking property access, props, events and semantic completion in template without compromising brevity and view-logic separation. Theoretically.
Practically this is an impossible approach. We need first reimplement vue's compiler and support expression parsing. More info here. vuejs/vue#4115 Evan did an amazing job to remove with
. However, removing with
requires patching Buble. Comparatively, we need to patch TypeScript compiler. Without microsoft/TypeScript#10160, it is almost impossible to catch up tsc's development which is developed by a full-time, talented team lead by one of programming language gods.
You can take a look at angular. They choose this approach, but does not work ideally. Currently angular forks typescript as ngc
, and checking templates require another level of AOT compiling. And every thing is achieved by command line, which means you need another tooling layer like angular-cli or custom webpack loader. A lot of building system complexity. And this is Angular team's outcome! A full-time, highly talented team from Google™! And a lot of community contributors (361 contributors, as a comparison, Vue has 47).
As a hobby project, I don't think it can get sufficient support for template compiling. Yet, the benefits you get from template checking is small.
from av-ts.
Firstly, thanks for your thoughtful analyze, it made it a bit more clear to me.
tsx/jsx files looks, as you say, more promising, and I hope the we soon will be able to skip the babel step too, but you summary makes it look even more attractive :-) I really should try to play more with this, and use .tsx
files in stead of .vue
, I mean ... we may not get every check, but we get quite a few.
I understand the template static compile problem (better now), and I guess that using tsx
will again provide us with a much better solution than the vue
model, in typescript context. Writing a complete compiler for things like that just don't make sense, as we don't gain anything the tsx don't provide us.
from av-ts.
I'm thinking about writing some render function helper to achieve similar type safety in plain TS. Something like this https://github.com/ktsn/vue-vnode-helper.
from av-ts.
Some idea about using vnode helper here. https://gist.github.com/HerringtonDarkholme/b3fccccf1abe7cde27686f613f53d55a
@druppy have you tried using vue's builtin createElement function to achieve type safety in template code? I wonder if it is a viable approach.
from av-ts.
For anyone interested, you can have a look on this.
https://github.com/HerringtonDarkholme/vivio
from av-ts.
Related Issues (20)
- Static class properties are undefined HOT 2
- howto no data props - question HOT 4
- Lifecycle methods give error TS1241 HOT 3
- @Component decorator requires `template` key HOT 3
- Compiler error watching nested optional value HOT 2
- p({type: String}) doesn't work as expected HOT 1
- Try to improve `p` definition. HOT 1
- Some webpack related issue started from v0.8.0 HOT 2
- Failing unit tests on PhantomJS since av-ts 0.8.x HOT 5
- TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. HOT 2
- @Lifecycle problem HOT 1
- Mixin type definition seems broken in ts 2.4 HOT 5
- Anything has to do to work with Vue 2.5? HOT 2
- @Data throws TS2345 about "Dict" when "strict: true" HOT 3
- Support `next(false)` in `beforeRouteEnter`
- how to write mapState function in vuex?? HOT 3
- TS3.0 introduced generic rest parameters HOT 3
- @Watch('value') warning: Argument type 'value' is not assignable to [string,string,string,string] HOT 1
- Is project alive? HOT 1
- Props throw `TypeError: Cannot redefine property: xxx` (with workaround) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from av-ts.