-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Resolved value from promise is always {} from compile time, cause error #6269
Comments
cannot repro locally using the latest bits on master: interface SomeType { x }
interface Service { getData(): Promise<SomeType> }
class A {
_someService: Service;
data: SomeType;
getData(){
this._someService.getData().then(data=>this.data=data);
}
}
class A {
getData() {
this._someService.getData().then(data => this.data = data);
}
} Can you check if your scenario has any specifics that reveal the problem? |
official angular2 site tutorial https://angular.io/docs/ts/latest/tutorial/toh-pt4.html export class AppComponent implements OnInit {
public title = 'Tour of Heroes';
public heroes: Hero[];
public selectedHero: Hero;
constructor(private _heroService: HeroService) { }
getHeroes() {
this._heroService.getHeroes().then(heroes => this.heroes = heroes);
}
ngOnInit() {
this.getHeroes();
}
onSelect(hero: Hero) { this.selectedHero = hero; }
} TS2332: length property missing, {} cannot be assigned to type Hero[] |
@vladima If my code is wrong, without ':Hero[]' there should be an error also and even before compiling, there's already an red underscore error mark from intellisense my environment is Windows 10 x64 + VS2015 + TypeScript 1.7.5 |
tsconfig - same as angular.io quickstart guide car.ts export interface Car {
id: number,
name: string
} car.service.ts import {Injectable} from 'angular2/core';
import {Car} from './car';
import {CARS} from './mock-cars';
@Injectable()
export class CarService {
getCars() {
return new Promise(resolve=>
setTimeout(() => resolve(CARS), 2000)
);
}
} mock.cars.ts import {Car} from './car';
export var CARS: Car[] = [
{ id: 1, name: 'BMW M4' },
{ id: 2, name: 'Audi A8' },
{ id: 3, name: 'LandRover Range Rover Sport' }
]; |
Quick tip @alanpurple - I've been fixing your examples, but you should surround your code samples with code fences. Start your code off with ````ts` and end it with `````. |
@DanielRosenwasser Yeah thanks for the tip :) |
I think the problem is that there's nothing we can infer for the type argument in getCars() {
// Notice the type argument 'Car[]'
return new Promise<Car[]>(resolve=>
setTimeout(() => resolve(CARS), 2000)
);
} I think that should take care of it. #5254 is probably related. |
@DanielRosenwasser Thx, solved, and sorry for not seeing correctly |
No problemo! It can be pretty subtle. |
it seems that type checking always fails except type 'any; for resolved value from Promise
public data:SomeType
getData(){
this._someService.getData().then(data=>this.data=data); // always emit TS2332 error
}
with
public data;
it works
The text was updated successfully, but these errors were encountered: