Typescript从实现中推断通用类型

时间:2018-11-22 08:31:59

标签: typescript typescript-typings

所以我正在做一个本机反应项目。 我希望有人能从实现中推断出泛型类型。

type itemType<T> = { item: T };

const someItem = {
    item: 'Some String'
};

type someItemType = typeof someItem;
// So Here someItemType will be {item: string}, i want it to be itemType<string> and 
// in this example i have implemented itemType but in real use case i want to infer 
//it from the actual implementation  

2 个答案:

答案 0 :(得分:2)

打字稿目前不支持对变量的部分推断。您唯一的选择是使用函数的推断行为:

<div class="modal fade" id="viewServiceModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
@using (Html.BeginForm("ServicesApprove", "Services", FormMethod.Post, new { @Id = "mainForm", @class = "edit_form" }))
{

<div class="modal-dialog modal-dialog-centered" style="min-width:60%" role="document">
    <input type="hidden" class="form-control editService_ServiceId" id="Id" name="Id" />
    <div class="modal-content">
        <div class="modal-header">
            <h3><label class="control-label editService_Name" for="Name"></label></h3>
        </div>
        <div class="modal-body">
            <div class="form-group">



                <div class="form-group">
                    <fieldset>
                        <div class="row">
                            <div class="col-6">
                                <label class="control-label" for="Desc"><strong>Description:</strong></label>
                                <textarea readonly class="form-control-plaintext editService_Description" name="Description" id="Description" type="text" style="resize:none; cursor:default;"></textarea>
                            </div>
                            <div class="col-6">
                                <label class="control-label" for="Attachment"><strong>Attachment:</strong></label>
                                <input readonly class="form-control-plaintext editService_Attachment" name="Attachment" id="Attachment" type="text" style="cursor:default;">
                            </div>
                        </div>
                    </fieldset>
                </div>

                <div class="form-group">
                    <fieldset>
                        <div class="row">
                            <div class="col-6">
                                <label class="control-label" for="RequestorId"><strong>Requested By:</strong></label>
                                <input readonly class="form-control-plaintext editService_Requestor" name="RequestorId" id="RequestorId" type="text" style="cursor:default;">
                            </div>
                            <div class="col-6">
                                <label class="control-label" for="CreatedDate"><strong>Submitted Date:</strong></label>
                                <input readonly class="form-control-plaintext editService_CreatedDate" name="CreatedDate" id="CreatedDate" type="text" style="cursor:default;">
                            </div>
                        </div>
                    </fieldset>
                </div>


            </div>
            <div class="modal-footer">

                <input type="submit" class="btn btn-success" value="Approve" formaction="ServicesApprove" />
                <input type="submit" class="btn btn-danger" value="Reject" formaction="ServicesReject" />

                <button type="button" class="btn btn-outline-danger" data-dismiss="modal">Cancel</button>
            </div>
        </div>
    </div>
</div>

}

仅需注意一点,在原始示例中将someItem键入为type itemType<T> = { item: T }; const createItemType = <T>(o: itemType<T>) => o; const someItem = createItemType({ //someItem is typed as itemType<string> item: 'Some String' }) 并不重要,因为打字稿使用结构兼容性来确定可分配性,因此它仍可分配给{item: string},。因此,如果结构兼容,就可以了:

itemType<string>

答案 1 :(得分:2)

因为TypeScript使用结构化类型,所以将类型定义为{ item: string }还是itemType<string>都没有关系。这意味着两者是相同的,因为它们具有相同的结构。

例如,您可以将任一类型的值分配给其他类型:

type itemType<T> = { item: T };

const someItem = {
    item: 'Some String'
};

type someItemType = typeof someItem;

const a: itemType<string> = { item: 'exmaple a' };
const b: someItemType = { item: 'exmaple b' };

let c: itemType<string>;

c = a;
c = b;

let d: someItemType;

d = a;
d = b;