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

Resolved value from promise is always {} from compile time, cause error #6269

Closed
alanpurple opened this issue Dec 27, 2015 · 10 comments
Closed
Labels
Duplicate An existing issue was already created Question An issue which isn't directly actionable in code

Comments

@alanpurple
Copy link

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

@vladima vladima added the Needs More Info The issue still hasn't been fully clarified label Dec 28, 2015
@vladima
Copy link
Contributor

vladima commented Dec 28, 2015

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);
    }
}

node built\local\tsc.js D:\sources\bugs\a.ts -t es6 yields no errors and produces the following javascript:

class A {
    getData() {
        this._someService.getData().then(data => this.data = data);
    }
}

Can you check if your scenario has any specifics that reveal the problem?

@alanpurple
Copy link
Author

@vladima

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
Copy link
Contributor

vladima commented Dec 29, 2015

still cannot repro. I've downloaded the sample from this link, added tsconfig.json and tried to build the code - still cannot see any errors. My repro bits can be found here, can you check if there are any differences in your code?

@alanpurple
Copy link
Author

@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

@alanpurple
Copy link
Author

image

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' }
];

@DanielRosenwasser
Copy link
Member

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 `````.

@alanpurple
Copy link
Author

@DanielRosenwasser Yeah thanks for the tip :)

@DanielRosenwasser
Copy link
Member

I think the problem is that there's nothing we can infer for the type argument in getCars. If you make your getCars the following:

    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.

@alanpurple
Copy link
Author

@DanielRosenwasser Thx, solved, and sorry for not seeing correctly

@DanielRosenwasser DanielRosenwasser added Duplicate An existing issue was already created Question An issue which isn't directly actionable in code and removed Needs More Info The issue still hasn't been fully clarified labels Dec 30, 2015
@DanielRosenwasser
Copy link
Member

No problemo! It can be pretty subtle.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants