Skip to content

wrong typing of "this" when returning concatenated string in computed #1554

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

Closed
3 tasks done
jbmaisner opened this issue Nov 26, 2019 · 8 comments
Closed
3 tasks done

Comments

@jbmaisner
Copy link

jbmaisner commented Nov 26, 2019

  • I have searched through existing issues
  • I have read through docs
  • I have read FAQ

Info

  • Platform: macOS
  • Vetur version: 0.22.6
  • VS Code version: 1.40.2

Problem

When I return a concat string + props in a computed, the keyword "this" is wrong typed in methods and lifecycle hooks. It's no more typed as "CombinedVueInstance" which causes some problems with Intellisense suggestions and type safe javascript through JSDoc.
Please see screenshots for better comprehension.

Wrong typing of keyword "this"
Capture d'écran 2019-11-26 16 03 34

Correct typing of keyword "this"
Capture d'écran 2019-11-26 16 03 19

Reproducible Case

You can simply insert the following code example,
Or create a SFC with any template, then in the script section, add a mounted hook and a lambda method. Finally add a computed returning a string + props.

Hover the keyword "this" in the mounted hook or in the method.

Code example

<template>
	<div/>
</template>

<script>
export default {
	props: {
		tata: {
			type: String,
			default: null
		}
	},

	computed: {
		titi () {
			return 'toto' + this.tata;
		}
	},

	mounted () {
		this.$el;
	},

	methods: {
		toto () {
			console.log(this.tata);
		}
	}
};
</script>
@kernZero
Copy link

+1 same problem on same platform

@octref
Copy link
Member

octref commented Feb 11, 2020

This seems to work fine for me (annotating the return type)

<template>
	<div></div>
</template>

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
	props: {
		tata: {
			type: String,
			default: null
		}
	},

	computed: {
		titi (): string {
			return 'toto' + this.tata;
		}
	},

	mounted () {
		this.$el;
	},

	methods: {
		toto () {
			console.log(this.tata);
		}
	}
})
</script>

I guess it's somewhat related to microsoft/TypeScript#30854. Need to file a new issue in TS...

@F0rsaken
Copy link

@jbmaisner @kernZero you can use JSDoc to annotate return types of computed and fix your issue

@octref
Copy link
Member

octref commented Jun 7, 2020

I can confirm, adding JSDoc annotation works. But upstream issue is closed as Working as Intended so there's nothing I can do.

@DanielRosenwasser This used to work as expected and fixing this would help a lot of beginner Vue users who don't know TS.

image

@DanielRosenwasser
Copy link
Contributor

Can you give it a shot on TS 3.9.5? There were a few small fixes that might have helped there.

@octref
Copy link
Member

octref commented Jun 16, 2020

@DanielRosenwasser I'm using [email protected], still doesn't work

<template>
  <div/>
</template>

<script>
import Vue from 'vue'

export default {
  props: {
    tata: {
      type: String,
      default: null
    }
  },

  computed: {
    /**
     * @returns {string}
     */
    titi () {
      return 'toto' + this.tata;
    }
  },

  mounted () {
    this.$el;
  },

  methods: {
    toto () {
      console.log(this.tata);
    }
  }
};
</script>

Once you remove the JSDoc, this fails to resolve its type.

@yoyo930021
Copy link
Member

I think duplicate of #1707 ?

@octref
Copy link
Member

octref commented Sep 4, 2020

Duplicate of #1707.

@octref octref closed this as completed Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants