Giter Club home page Giter Club logo

Comments (5)

RyanCavanaugh avatar RyanCavanaugh commented on September 27, 2024 1

If you can write "some string".length then string has a length property, so a string is a { length: number } even though it's not an object with a length property. You can try to slice this some other way but it's not clear that it's more intuitive.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on September 27, 2024

The error is correct; these narrowings are not equivalent. Knowing that T isn't string isn't sufficient proof that it's object. Consider something like this:

const fn1 = <T extends string | { toFixed(): string }>(v: T) => {
  if (typeof v === 'string') {
    return { text: v };
  }

  const { ...rest } = v;

  return { text: '???', ...rest };
};
fn1(42);

from typescript.

G-Rath avatar G-Rath commented on September 27, 2024

Right, so effectively this is me forgetting that this is "types on a dynamic runtime" rather than Go or Rust or whatever where it could only be those two types - my general thinking was it sufficient as I'd told TS its either "x or y" and then checked if it was "y" so the opposite branch should then be "x"...

I'm trying to think why that trips me up though - I feel like TS does behave like this elsewhere, but maybe I'm just misremembering some subtilties 🤔

(also I realised I've left out my truthy check which'd mean the whole "null is an object" thing in theory shouldn't be relevant - I'm pretty sure that doesn't matter for my simple example, but would for more real-world code)

from typescript.

G-Rath avatar G-Rath commented on September 27, 2024

actually maybe this is more about TS not having a difference between "pure data type object thing" and ... idk what to call the alternative - things with methods.? it's not that though but just i.e. in my head I'm thinking "this function takes a plain object with some properties", but you've shown TS actually checks for something slightly different meaning depending on your type something like a primitive could be considered valid for what looks like an object type, and then boom here we are...

from typescript.

G-Rath avatar G-Rath commented on September 27, 2024

That makes sense - it's that an "object" is different from "a value with properties", and that really TS's syntax describes the latter rather than the former (which makes sense because that's the underlying principle of duck typing, which is what JS is doing).

Happy for this to be closed, and thanks for working this through with me 🙂

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.