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

In JS, @type for class property declarations is ignored unless the default value is contained in parentheses #26938

Closed
AlCalzone opened this issue Sep 6, 2018 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@AlCalzone
Copy link
Contributor

TypeScript Version: 3.1.0-dev.20180906

Search Terms: js class property type

Code

class Foo {
	constructor() {
		/** @type {Record<string, number>} */
		this.bar = {}; // has type {[x: string]: any}

		/** @type {Record<string, number>} */
		this.baz = ({}); // has type {Record<string, number>}
	}
}

Expected behavior:
bar and baz both have the declared type.
Actual behavior:
Only baz has the declared type.

Related Issues: possibly 26743

@ghost
Copy link

ghost commented Sep 6, 2018

This seems to be working for me in typescript@next -- this.bar.foo = ""; will fail with Type '""' is not assignable to type 'number'..

@ghost ghost added the Needs More Info The issue still hasn't been fully clarified label Sep 6, 2018
@AlCalzone
Copy link
Contributor Author

I must be doing something wrong then

// package.json
{
  "name": "repro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "typescript": "^3.1.0-dev.20180906"
  }
}

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
  }
}

unbenannt3
unbenannt
unbenannt2

@ghost
Copy link

ghost commented Sep 6, 2018

I did reproduce the error with [email protected]. Are you sure your editor is set up to use typescript@next?

@AlCalzone
Copy link
Contributor Author

Thanks, that works indeed. Its kinda hard to switch versions in a js-only project due to the missing button with the TS version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

1 participant