Skip to content

[@types/ember-data enhancement] - DS.Store.createRecord() doesn't type check inputProperties #251

Open
@ghost

Description

ember-cli: 3.3.0
node: 8.11.3
os: linux x64
TypeScript 2.9.2

My tsconfig.json

{
"compilerOptions": {
"target": "es2017",
"allowJs": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"allowUnreachableCode": false,
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noEmitOnError": false,
"noEmit": true,
"inlineSourceMap": true,
"inlineSources": true,
"baseUrl": ".",
"module": "es6",
"paths": {
"my-app/tests/": [
"tests/
"
],
"my-app/": [
"app/
"
],
"": [
"types/
"
]
}
},
"include": [
"app",
"tests",
"types"
]
}

My tslint.json

{
"defaultSeverity": "error",
"extends": [
"tslint:recommended",
"tslint-config-prettier"
],
"rules": {
"arrow-parens": [true, "ban-single-arg-parens"],
"cyclomatic-complexity": [true, 7],
"forin": false,
"indent": [true, "spaces", 2],
"interface-name": false,
"max-file-line-count": [true, 500],
"member-ordering": [true, {
"order": [
"public-static-field",
"public-static-method",
"public-instance-field",
"public-constructor",
"public-instance-method",
"protected-static-field",
"protected-static-method",
"protected-instance-field",
"protected-constructor",
"protected-instance-method",
"private-static-field",
"private-static-method",
"private-instance-field",
"private-constructor",
"private-instance-method"
]
}],
"no-console": false,
"no-empty": [true, ["allow-empty-catch", "allow-empty-functions"]],
"object-literal-sort-keys" : false,
"quotemark": [true, "single", "avoid-escape", "avoid-template"],
"semicolon": [true, "never"],
"trailing-comma": [true, "never"],
"variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"]
}
}

How to reproduce?

Use DS.Store.createRecord() to create a new record and pass inputProperties that don't exist on the DS.Model subclass:

this.store.createRecord('post', { attributeThatDoesNotExist: 'value' })

What did you expect to see?

Expectation is to get the same compiler errors as generated from the following code:

const post = this.store.createRecord('post')
post.setProperties({ attributeThatDoesNotExist: 'value' })

What happened instead?

There are no type errors emitted when createRecord() is called with invalid properties or with values of the incorrect type.

Activity

mike-north

mike-north commented on Sep 4, 2018

@mike-north
Contributor

Fix is:

Change this
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/034c9126166528c659935f624b17b911ae5b6b15/types/ember-data/index.d.ts#L1013-L1016

            createRecord<K extends keyof ModelRegistry>(
                modelName: K,
                inputProperties?: {}
            ): ModelRegistry[K];

to

            createRecord<K extends keyof ModelRegistry>(
                modelName: K,
                inputProperties?: Partial<SOME_UNWRAP_THING<ModelRegistry[K]>>
            ): ModelRegistry[K];

This might mean we should expose some of @types/ember's utility types via a ghost module so that they may be consumed in libs like @types/ember-data.

changed the title [-][bug] - DS.Store.createRecord() doesn't type check inputProperties[/-] [+][@types/ember-data bug] - DS.Store.createRecord() doesn't type check inputProperties[/+] on Sep 6, 2018
changed the title [-][@types/ember-data bug] - DS.Store.createRecord() doesn't type check inputProperties[/-] [+][@types/ember-data enhancement] - DS.Store.createRecord() doesn't type check inputProperties[/+] on Sep 6, 2018
mike-north

mike-north commented on Sep 6, 2018

@mike-north
Contributor

flagging this as an enhancement because the request is for additional type checking that doesn't currently exist (as opposed to TS getting in the way and blocking you from doing things)

chriskrycho

chriskrycho commented on Sep 28, 2023

@chriskrycho
Member

Leaving this open at present as I don't know whether it has changed in the interval (I don't expect so)… but the preferred path forward here is almost certainly going to be the exposing the significantly-different design path Ember Data has taken in the intervening years via types shipped from Ember Data directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementtypes:coreSomething is wrong with the Ember type definitionstypes:core:dataSomething is wrong with the Ember Data type definitions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mike-north@chriskrycho

        Issue actions

          [@types/ember-data enhancement] - DS.Store.createRecord() doesn't type check inputProperties · Issue #251 · typed-ember/ember-cli-typescript