Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript may be can not infer the props type #6924

Closed
xingyesh opened this issue Oct 26, 2017 · 8 comments
Closed

Typescript may be can not infer the props type #6924

xingyesh opened this issue Oct 26, 2017 · 8 comments

Comments

@xingyesh
Copy link

xingyesh commented Oct 26, 2017

Version

vue 2.5.2
typescript 2.5.3

Reproduction link

https://jsfiddle.net/chrisvfritz/50wL7mdz/typescript

Steps to reproduce

Typescript may be can not infer the props type. eg:

//App.vue
<template>
    <div>
        <vheader :test="test" :type="type"></vheader>
    </div>
</template>
<script lang="ts">
import header from './header.vue';
export default {
    components: {
        vheader: header
    },
    computed: {
    },
    methods: {
        test(left: number, right: number): number {
            return left   right;
        }
    }
}
</script>
//header.vue
<script lang="ts">
import Vue from 'vue'
export default {
    props: {
        test: Function
    },
    data() {
        this.test('aa','aa');
        return {}
    }
}
</script>

this.test('aa', 'aa') can invoke correct.
Now Vue can not get the parent's(props) function type?

What is expected?

types error.

What is actually happening?

can run success.

@ktsn
Copy link
Member

ktsn commented Oct 26, 2017

test's type in your code is successfully inferred as Function type. That's expected behavior.
We cannot infer the actual function signature because props option does not have such information.

If you want to strict it, you need to manually annotate your props type vid PropType that would be introduced via #6856

@ktsn ktsn closed this as completed Oct 26, 2017
@xingyesh
Copy link
Author

xingyesh commented Oct 26, 2017

Thanks a lot.

<script lang="ts">
import Vue from 'vue'
interface setFunction {
    (argu1: number, argu2: number):string
}
export default {
    props: {
        test: Object as (() => setFunction)
    },
    data() {
        this.test(1,1);
        return {}
    }
}
</script>

After I reference #6865, the this.test can get the correct parameter types
and I have an other question, when we use component trees, Cannot we through $props, $refs or other Vue options API to get the contextual type? such as , perhaps we can top-down or down-top to infer all the Types.

@ktsn
Copy link
Member

ktsn commented Oct 26, 2017

Currently, no.

  1. In any cases, typescript compiler cannot analyze template. We need to make additional type checker if we want to check it.
  2. For TSX/render function, we may be able to type check props type from a parent component. There is a discussion thread TypeScript: add Props type to component constructor #6901

@xingyesh
Copy link
Author

Thanks. I will learn it.

@namse
Copy link

namse commented Aug 27, 2018

Angular's AOT Compiler can check template's type also with fullTemplateTypeCheck option.

So I think Vue's template compiler can check template's type.
@ktsn @yyx990803 May I ask you whether there is any movement to make it possible?

@ktsn
Copy link
Member

ktsn commented Aug 27, 2018

FYI vuejs/vetur#681

@namse
Copy link

namse commented Aug 27, 2018

@ktsn That's so great job! Is it only work on vetur? Is it possible to run with Vue Cli's lint?

@chriszrc
Copy link

Yeah, I'm still sticking with Atom, even though MS bought Github and technically now owns Atom too... It's critical to have type checking in templates, I find that currently it's the main source of typos/bugs in my typescript vue apps-

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants